This is ok as here:
# declare nameref without assigned value
declare -n ref
# use nameref as iterator
for ref in foo bar baz
do ref='Hello the World!'
done
declare -p foo bar baz
although: declare -n ref
leaves the ref variable in a limbo state with no value
$ unset -n ref; declare -n ref; printf %q\\n "$ref"
''
This strangely cause no error and returns an empty string.
$ unset -n ref; declare -n ref; printf %q\\n "${!ref}"
bash: ref: invalid indirect expansion
But trying to expand the value of the nameref itself causes this
"invalid indirect expansion error"
This seems counter-intuitive.
Intuitively:
- Expanding the value of a nameref without an assigned value should
return an empty string.
- Expanding the value of the refered variable whose nameref is undefined
would return some error
The other related issue is that this limbo empty state of a nameref is
only obtained with an initial `declare -n ref`.
There is no way to later clear or assign an empty string to a nameref
unless destroying and recreating the nameref with:
unset -n ref
declare -n ref
--
Léa Gris