bug-bash
[Top][All Lists]
Advanced

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

Re: local_array_declare_-g suggested workaround


From: Chet Ramey
Subject: Re: local_array_declare_-g suggested workaround
Date: Wed, 26 Dec 2012 19:11:54 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:17.0) Gecko/17.0 Thunderbird/17.0

On 12/20/12 5:09 PM, Vicente Couce Díaz wrote:
> Bash 4.2.36(1)-release (i486-pc-linux-gnu), Debian wheezy (bash 4.2-4)
> 
> Given this example:
> 
> f() {
>         local -a v
>         local -a w
>         g
>         echo "f: ${v[@]}, ${w[@]}"
> }
> g() {
>         aux=v
>         declare -ga "$aux=( asdf fdsa )"
>         declare -ga w=( asdf fdsa )
>         echo "f: ${v[@]}, ${w[@]}"
> }
> f
> echo "FIN: ${v[@]}, ${w[@]}"
> 
> Result:
> 
> g: , asdf fdsa
> f: , asdf fdsa
> FIN: asdf fdsa,
> 
> Expected:
> 
> g: asdf fdsa, asdf fdsa
> f: asdf fdsa, asdf fdsa
> FIN: ,
> 
> If I declare a variable local in a function, then it should be kept in the
> same scope when using 'declare -g' in a child context. This happens with
> $w, but not with $v, in this case the variable becomes global and no more
> local to the f call, so hidden there by the local homonyme.
> This happens when employing -a and -g and the only difference is that the
> definition is quoted.

That's not the way it should be.  declare -g exists to create variables at
the global scope.  It doesn't have any effect on the shell's variable
scoping rules or behavior.

`v' and `w' should be created as arrays at the global scope, and according
to the current shell behavior, which does not skip local variables that
have not been assigned a value when doing variable lookup (even though,
technically, that means they're unset), should be masked by the local
declaration in f.

I have some work to do in this area.

Chet

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



reply via email to

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