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:16:54 -0400
User-agent: Mozilla Thunderbird

On 3/11/24 8:26 AM, Adrian Ho wrote:
Thanks much for all the insights, everyone! Indeed, the man page isn't
clear on how to reason about `local -g`. I'm now left with the following
understanding:

1. There is *nothing local* about `local -g`; it operates entirely at the
global level.

It exists to allow shell functions to create variables at the global scope
with attributes that require the use of `declare', which is not possible
without `-g'.


In particular, it does *not* create a local reference to a
global var, which is the natural assumption given the typical behaviour of
`declare/local`.

I would disagree that this assumption is `natural'.


2. Because of this, there is no way to *read* a global var if a calling
scope declares a shadowing var, short of unsetting that shadow var.

This is how dynamic scoping works.


The `-g` option forces variables to be created or modified at the global
scope, even when `declare` is executed in a shell function.

to:

The `-g` option forces variables to be created or modified at the global
scope, even when `declare` is executed in a shell function, *and no
references to these variables are created in that function's scope*.

I disagree that this particular addition is needed. I'd be happy to
entertain other suggestions about how to clarify the documentation.

Chet
--
``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]