I should add the related problem: $ f() { declare -n x=$1; echo "x=$x"; declare y="local Y"; echo "x=$x"; } $ y="global Y" $ f y x=global Y x=local Y
In other words, even if you do get a nameref to point to something in the caller's scope, as soon as you shadow that variable with another local variable, the nameref points to that one instead. (As Greg pointed out, the name ref basically fails if the referenced name is shared with any local variable in the function.) This is arguably in line with the name of the feature ("nameref", i.e. referencing the variable by name rather than by specific identity) but I feel it's much more useful for a variable reference, once established, to be stable. To sum up a bit: - There should be a way to declare a nameref locally in a function which reliably refers to a variable in the caller's scope - IMO this is the whole point of having a reference type, to get around the fact that the language is otherwise pass-by-value. - A nameref, once established, should refer to a particular name _in a particular scope_. Shadowing the referenced variable with a new local should not cause the nameref to switch to the new definition. - Because Bash can create global namerefs from within a function, creating a global nameref to a function-local variable should not be allowed (because the global nameref would outlive the declaration it references)