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: Chet Ramey
Subject: Re: "local -g" declaration references local var in enclosing scope
Date: Mon, 11 Mar 2024 11:45:17 -0400
User-agent: Mozilla Thunderbird

On 3/11/24 12:08 AM, Kerin Millar wrote:

Speaking of which, to do both of these things has some interesting effects ...

$ z() { local -g a; unset -v a; a=123; echo "innermost: $a"; }; unset -v a; x; 
declare -p a
innermost: 123
inner: 123
outer: 123
declare -- a

$ z() { local -g a; unset -v a; unset -v a; a=123; echo "innermost: $a"; }; 
unset -v a; x; declare -p a
innermost: 123
inner: 123
outer: 123
declare -- a="123"

These show the normal effects of unset combined with dynamic scoping. This
ability to unset local variables in previous function scopes has been the
subject of, um, spirited discussion.

$ x() { local a; y; local +g a; a=456; echo "outer: $a"; }; unset -v a; x; 
declare -p a
innermost: 123
inner: 123
outer: 456
declare -- a="123"

Let's assume the previous function definitions remain in effect here. In
that case, z continues to unset the local variable definitions in previous
scopes, but the `local +g a' is basically a no-op -- it implies not using
the global scope for a, which is the same as not using the option at all.

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature


reply via email to

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