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: Chet Ramey
Subject: Re: for ((i=0; i<3 && i!=1; i++))
Date: Mon, 12 Jan 2015 11:40:07 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:31.0) Gecko/20100101 Thunderbird/31.3.0

On 1/12/15 4:29 AM, l_j_f wrote:
> 1. bash version
> -sh-4.3# bash --version
> GNU bash, version 4.3.0(1)-release (arm-hisiv200-linux-gnu)
> Copyright (C) 2013 Free Software Foundation, Inc.
> License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
> This is free software; you are free to change and redistribute it.
> There is NO WARRANTY, to the extent permitted by law.
>  
> 2. the script
> #!/bin/bash
>  
> main() {
>         local i        
>                                                          
>         for ((i=0; i<3; i++)); do                        
>                 local var                                
>                 if [[ "${i}" == "1" ]]; then
>                         var=1
>                 fi     
>                        
>                 echo i=$i var=$var
>         done
> }        
>          
> main "$@"
>  
> 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=

Why? You never unset var or set it to the empty string after it's set to 1.
Shell variables don't have `loop scope'.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    address@hidden    http://cnswww.cns.cwru.edu/~chet/



reply via email to

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