bug-bash
[Top][All Lists]
Advanced

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

Re: "local -g" declaration references local var in enclosing scope


From: Zachary Santer
Subject: Re: "local -g" declaration references local var in enclosing scope
Date: Sun, 10 Mar 2024 15:55:07 -0400

On Sun, Mar 10, 2024 at 1:51 PM Kerin Millar <kfm@plushkava.net> wrote:
> $ y() { local -g a; a=123; echo "inner: $a"; }
> $ x; echo "outermost: $a"
> inner: 123
> outer: 123
> outermost:
>
> This may not be. There, the effect of the -g option effectively ends at the 
> outermost scope in which the variable, a, was declared. Namely, that of the x 
> function.

Pretty sure what's going on there is that "a=123" here is just setting
the value of the variable a from the innermost scope where it's
declared, and has nothing to do with declaring a different, global
variable a immediately prior.

Relatedly, how would one set attributes on a variable declared in a
calling function? 'readonly' and 'export' can do it for their
respective attributes, but otherwise, I think you just can't.



reply via email to

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