2

In the manual page for bash, the indirect expansion for a parameter that is not a nameref is explained as

If the first character of parameter is an exclamation point (!), and parameter is not a nameref, it introduces a level of indirection. Bash uses the value formed by expanding the rest of parameter as the new parameter; this is then expanded and that value is used in the rest of the expansion, rather than the expansion of the original parameter. This is known as indirect expansion. The value is subject to tilde expansion, parameter expansion, command substitution, and arithmetic expansion.

But I am not able to replicate the last sentence.

Setting x=y, y=z and z=end. The result is as expected.

$ echo ${!x}
z

Setting x=y, y='$z' and z=end.

$ echo ${!x}
$z

Setting x='$y', y=z, and z=end.

$ echo ${!x}
-bash: $y: invalid variable name

So, what value is subject of expansions as mentioned above in the documentation? What am I missing?

(Bash version used is 5.1.0(1)-release (x86_64-redhat-linux-gnu)).

1
  • The part where it says "Bash uses the value formed by expanding the rest of parameter as the new parameter; this is then expanded and that value is used in the rest of the expansion" also sounds a bit odd. It made me try something like "${!$foo}", but that also gives "bad substitution". (Maybe for the better.)
    – ilkkachu
    Commented Apr 5, 2022 at 13:16

1 Answer 1

3

It sounds like a documentation bug. The commit that introduced that text goes rather in the opposite direction:

Quoting the related additions to CWRU/CWRU.log in the source distribution:

+                                  12/18
+                                  -----
+subst.c
+       - parameter_brace_find_indir: when expanding the indirect parameter
+         to find the eventual variable name, we don't perform word splitting.
+         Make sure this does the right thing for * and @.  Fixes bug
+         reported by isabella parakiss <[email protected]>
+
+                                  12/19
+                                  -----
+doc/{bash.1,bashref.texi}
+       - indirect expansion: make sure to note that the value of the indirect
+         variable does not undergo word splitting as one of its expansions,
+         as in fix from 12/18

That text is probably missing a not: The value is not subject....

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .