bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: for ((i=0; i<3 && i!=1; i++))


From: Eric Blake
Subject: Re: for ((i=0; i<3 && i!=1; i++))
Date: Mon, 12 Jan 2015 09:34:37 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0

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

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]