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 18:31:03 +0100

On Thu, Mar 14, 2024, 15:16 Zachary Santer <zsanter@gmail.com> wrote:

> On Thu, Mar 14, 2024 at 9:54 AM Greg Wooledge <greg@wooledge.org> wrote:
> >
> > I don't quite understand what this is saying.  Do the variables have
> > different names, or the same name?  If they have different names, then
> > the nameref shouldn't "hide" the other variable.  But they can't have
> > the same name, because a nameref pointing to itself is a circular
> > reference and won't ever work under any circumstance.
> >
> > hobbit:~$ f() { local -n var=var; var=1; }; f
> > bash: local: warning: var: circular name reference
> > bash: warning: var: circular name reference
> > bash: warning: var: circular name reference
> >
> > You don't even need an outer calling function to see this.
> >
>
> Editing things out of my original email to focus on this point:
>
> On Sun, Mar 10, 2024 at 7:29 PM Zachary Santer <zsanter@gmail.com> wrote:
> >
> > $ cat ./nameref-what
> > #!/usr/bin/env bash
> >
> > func_1 () {
> >   local var_3='EGG'
> >   func_2
> >   printf '%s\n' "func_1:"
> >   local -p var_3
> > }
> >
> > func_2 () {
> >   local -n nameref_3='var_3'
> >   nameref_3='soufflé'
> >   local var_4='GROUND BEEF'
> >   local -n nameref_4='var_4'
> >   local -l nameref_4
> >   printf '%s\n' "func_2:"
> >   local -p nameref_3
> >   local -p var_3
> >   local -p nameref_4
> >   local -p var_4
> > }
> >
> > func_1
> >
> > $ ./nameref-what
> > func_2:
> > declare -n nameref_3="var_3"
> > ./nameref-what: line 31: local: var_3: not found
> > declare -n nameref_4="var_4"
> > declare -l var_4="GROUND BEEF"
> > func_1:
> > declare -- var_3="soufflé"
>
> Not on a machine with bash right now. 'declare -p var_3' in func_2 ()
> said var_3 was not found, despite it having just been set by assigning
> a value to the nameref variable nameref_3.
>

u didnt -g so it went

>


reply via email to

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