On 2/16/11 5:07 AM, Clark J. Wang wrote: > See following script result: > > bash# cat declare-g.sh > #!/bin/bash > > var=global > > f1() > { > declare var=local > > f2 > echo 3.$var > } > > f2() > { > declare -g var > > echo 1.$var > var=global-changed > echo 2.$var > } > > f1 > echo 4.$var > bash# /usr/local/bash-4.2.0/bin/bash declare-g.sh > 1.local > 2.global-changed > 3.global-changed > 4.global > bash# > > I think the right result should be: > > 1.global > 2.global-changed > 3.local > 4.global-changed
The -g option exists solely to create variables at the global scope. The intent is that functions be able to declare global variables with attributes if they desire. It doesn't change the scoping rules or variable resolution behavior. 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/