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 c...@case.edu http://cnswww.cns.cwru.edu/~chet/