[prev in list] [next in list] [prev in thread] [next in thread]
List: bash-bug
Subject: Re: variable scope
From: Chet Ramey <chet.ramey () case ! edu>
Date: 2008-10-24 18:30:53
Message-ID: 4902145D.8060900 () case ! edu
[Download RAW message or body]
Antonio Macchi wrote:
> $ a=OUTSIDE
>
> $ f1 () { local a=INSIDE; f2; }
>
> $ f2 () { echo "before: $a";
> unset a;
> echo "after: $a"; }
>
>
> $ f3 () { local a=INSIDE;
> echo "before: $a";
> unset a;
> echo "after: $a"; }
>
>
> $ f1
> before: INSIDE
> after: OUTSIDE
>
> $ f3
> before: INSIDE
> after:
>
>
> I can unset an 'external-local' variable, and then get his global scope
> but I can't do the same with an 'internal-local' one
>
> may be this is not perfectly coherent
This was a conscious design choice.
Local variables have a scope restricted to the function in which they're
declared and its descendants in the call tree. Local variables shadow
global variables with the same name. View it as a chain of symbol
tables searched from most recent back. When a descendant unsets a
variable it's inherited from it's caller, whether or not that variable
is local in its caller, it uncovers earlier-declared incarnations.
The choice to make a variable declared as local persist as local after
being was explicit, after I got a bunch of bug reports doing it the
other way. Folks wanted
f()
{
local a
foo
unset a
bar
a=biz
qux
}
to result in the assignment to a creating a local variable. In the end,
it may or may not have been the right choice, but that's the behavior
we have.
Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
Chet Ramey, ITS, CWRU chet@case.edu http://cnswww.cns.cwru.edu/~chet/
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic