I'm reading "Bash Guide for Beginners". It says:
If the first character of
PARAMETER
is an exclamation point, Bash uses the value of the variable formed from the rest ofPARAMETER
as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value ofPARAMETER
itself. This is known as indirect expansion.
The example given is:
franky ~> echo ${!N*} NNTPPORT NNTPSERVER NPX_PLUGIN_PATH
I don't quite understand here:
the value of the variable formed from the rest of
PARAMETER
As the PARAMETER
is just !N*
, then
the rest of
PARAMETER
is just N*
. How could this form a variable? Did Bash search all possible command there?
Yes, it searches for all possible expansions of variables after the !. If you had done:
you would get only
NPX_PLUGIN_PATH
.Consider the following example:
If you read the
bash
man page, it basically confirms what you have stated:However, reading on from there:
In other words, your particular example
${!N*}
is an exception to the rule you quoted. It does, however, work as advertised in the expected cases, such as:You've hit an exception in indirection processing, where if the last character is
*
, all variables that have the prefix given before will be returned.There appears to be an exception when the given "indirection" ends in a
*
, as it does here. In this case, it gives all variable names that start with the part you specified (N
here). Bash can do that because it tracks variables and knows which ones exist.True indirection is this:
Say I have a variable
$VARIABLE
set to42
, and I have another variable$NAME
set toVARIABLE
.${!NAME}
will give me42
. You use the value of one variable to tell you the name of another: