bug-bash
[Top][All Lists]
Advanced

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

Re: variable scope


From: Chet Ramey
Subject: Re: variable scope
Date: Fri, 24 Oct 2008 14:30:53 -0400
User-agent: Thunderbird 2.0.0.17 (Macintosh/20080914)

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/




reply via email to

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