bug-bash
[Top][All Lists]
Advanced

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

Re: nameref and referenced variable scope, setting other attributes (was


From: alex xmb sw ratchev
Subject: Re: nameref and referenced variable scope, setting other attributes (was "local -g" declaration references local var in enclosing scope)
Date: Thu, 14 Mar 2024 08:27:33 +0100

how to unset a nameref

On Wed, Mar 13, 2024, 20:44 Chet Ramey <chet.ramey@case.edu> wrote:

> On 3/10/24 7:29 PM, Zachary Santer wrote:
>
> > Bash Version: 5.2
> > Patch Level: 26
> > Release Status: release
> >
> > Description:
> >
> > On Sun, Mar 10, 2024 at 3:55 PM Zachary Santer <zsanter@gmail.com>
> wrote:
> >>
> >> 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.
> >
> > Second-guessed myself.
> >
> > The manual says about 'declare -n':
> > -n  Give each name the nameref attribute, making it a name reference to
> > another variable. That other variable is defined by the value of name.
> All
> > references, assignments, and attribute modifications to name,  except
> those
> > using or changing the -n attribute itself, are performed on the variable
> > referenced by name's value. The nameref attribute cannot be applied to
> > array variables.
> >
> > local, when called on a nameref variable referencing a variable declared
> in
> > a calling function, creates a new local variable named the same as the
> > value of the nameref variable. Given the above, I would expect it to
> > instead allow the setting of attributes and value of a variable declared
> in
> > a calling function.
>
> `local' always creates variables at the current scope, or at the global
> scope if `-g' is supplied. If it's supplied the name of a nameref, it first
> resolves the nameref to find the name of the variable it's supposed to act
> on, failing if it can't. Once it has the name it needs, it creates or
> modifies the variable at the current scope. It doesn't try to create or
> modify the variable at the nameref's scope. This is one consequence of
> dynamic  scoping that affects the implementation: a nameref's value is just
> a name, not a pointer to a specific instance of a variable. Once you have
> that name, the normal scoping rules apply.
>
> If you want to look at it from a filesystem perspective, a nameref is a
> symlink, rather than a hard link.
>
> --
> ``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/
>
>
>


reply via email to

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