On 01/12/2015 02:29 AM, l_j_f wrote: > for ((i=0; i<3; i++)); do > local var > if [[ "${i}" == "1" ]]; then > var=1 > fi
> 3. the result > -sh-4.3# ./test3.sh > i=0 var= > i=1 var=1 > i=2 var=1 > # I think it should be > i=0 var= > i=1 var=1 > i=2 var= You seem to be under the impression that 'local' inside of a loop creates something local to the loop. However, that is not how bash works. 'local' creates something local to the function scope. Declaring something local through multiple iterations does not change the scope; the third loop iteration is not changing the value of the variable, and since the variable is still in scope from the first iteration, the behavior you see is correct. -- Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature