The nameref resolution search doesn’t re-start back at the original
context. It’s like a symbolic link — it continues at the same context as
the resolved nameref.

If the assignment to the global variable is intentional, then shouldn’t
expansion use the global variable’s value as well?

The expansion part really seems like the bug here, and can be demonstrated
without any crazy cross-scope chains.

As I understand it, the intention is indeed to allow namerefs to point to
outer-scope variables, even if they happen to have the same name as the
reference, since a function like

add_X() { local -n ref=$1; ref+=X; }

works correctly even if it is passed ref as the argument:

$ ref=; add_X ref; echo "$ref"
X

However, if we try to access $ref inside the function, we get an error,
though the += operation works just fine:

$ add_X_echo() { local -n ref=$1; ref+=X; echo "$ref"; }
$ ref=; add_X_echo; echo "$ref"
bash: warning: ref: circular name reference

X

​

Reply via email to