[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: "local -g" declaration references local var in enclosing scope
From: |
Kerin Millar |
Subject: |
Re: "local -g" declaration references local var in enclosing scope |
Date: |
Mon, 11 Mar 2024 04:17:17 +0000 |
On Sun, 10 Mar 2024 17:36:13 -0400
Greg Wooledge <greg@wooledge.org> wrote:
> On Sun, Mar 10, 2024 at 04:01:10PM -0400, Lawrence Velázquez wrote:
> > Basically, without an assignment, "local -g" does nothing.
>
> Well, the original purpose of -g was to create variables, especially
> associative arrays, at the global scope from inside a function.
>
> I think this thread has been asking about a completely different
> application, namely to operate upon a global scope variable from
> within a function where a non-global scope variable is shadowing the
> global one.
>
> (As far as I know, there isn't any sensible way to do that in bash.)
I agree. Given that bash does not support static scoping, it seems only
sensible to suggest that one simply refrains from trying to do that.
>
>
> Here it is in action. "local -g" (or "declare -g") without an assignment
> in the same command definitely does things.
>
> hobbit:~$ f() { declare -g var; var=in_f; }
> hobbit:~$ unset -v var; f; declare -p var
> declare -- var="in_f"
>
>
> I think the key to understanding is that while "local -g var" creates
> a variable at the global scope, any references to "var" within the
> function still use the standard dynamic scoping rules. They won't
> necessarily *see* the global variable, if there's another one at a
> more localized scope.
--
Kerin Millar
- Re: ${var@A}; hypothetical, related parameter transformations, (continued)
- Re: nameref and referenced variable scope, setting other attributes (was "local -g" declaration references local var in enclosing scope), Mike Jonkmans, 2024/03/19
- Re: nameref and referenced variable scope, setting other attributes (was "local -g" declaration references local var in enclosing scope), Chet Ramey, 2024/03/19
- Re: nameref and referenced variable scope, setting other attributes (was "local -g" declaration references local var in enclosing scope), Mike Jonkmans, 2024/03/19
- Re: "local -g" declaration references local var in enclosing scope, Chet Ramey, 2024/03/11
Re: "local -g" declaration references local var in enclosing scope, Lawrence Velázquez, 2024/03/10
Re: "local -g" declaration references local var in enclosing scope,
Kerin Millar <=
Re: "local -g" declaration references local var in enclosing scope, Kerin Millar, 2024/03/11
Re: "local -g" declaration references local var in enclosing scope, Adrian Ho, 2024/03/11
Re: "local -g" declaration references local var in enclosing scope, Chet Ramey, 2024/03/11
Re: "local -g" declaration references local var in enclosing scope, Chet Ramey, 2024/03/11
Re: "local -g" declaration references local var in enclosing scope, Kerin Millar, 2024/03/11
"local -g" declaration references local var in enclosing scope, aho+lex, 2024/03/10