|
From: | Greg Wooledge |
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 10:46:43 -0400 |
On Thu, Mar 14, 2024 at 10:15:35AM -0400, Zachary Santer 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. I can't seem to duplicate this. This is with bash 5.2: hobbit:~$ cat foo #!/bin/bash outer() { local var3=EGG inner } inner() { local -n ref3=var3 ref3=BANANA declare -p ref3 declare -p var3 } outer hobbit:~$ ./foo declare -n ref3="var3" declare -- var3="BANANA" And I get the same result in older versions as well: hobbit:~$ bash-5.1 foo declare -n ref3="var3" declare -- var3="BANANA" hobbit:~$ bash-5.0 foo declare -n ref3="var3" declare -- var3="BANANA" hobbit:~$ bash-4.4 foo declare -n ref3="var3" declare -- var3="BANANA" hobbit:~$ bash-4.3 foo declare -n ref3="var3" declare -- var3="BANANA"
[Prev in Thread] | Current Thread | [Next in Thread] |