\$${a}
在shell中,引用一个变量var用$var,那如何引用一个变量的变量呢?普通的用法是通过\$${a}来引用,同时通常用eval来处理。
eg.
a=letter_of_alphabet # Variable "a" holds the name of another variable.
letter_of_alphabet=z
echo
# Direct reference.
echo "a = $a" # a = letter_of_alphabet
# Indirect reference.
eval a=\$$a
echo "Now a = $a" # Now a = z
t=table_cell_3
table_cell_3=24
echo "\"table_cell_3\" = $table_cell_3" # "table_cell_3" = 24
echo -n "dereferenced \"t\" = "; eval echo \$$t # dereferenced "t" = 24
# In this simple case, the following also works (why?).
# eval t=\$$t; echo "\"t\" = $t"
echo
t=table_cell_3
NEW_VAL=387
table_cell_3=$NEW_VAL
echo "Changing value of \"table_cell_3\" to $NEW_VAL."
echo "\"table_cell_3\" now $table_cell_3"
echo -n "dereferenced \"t\" now "; eval echo \$$t
# "eval" takes the two arguments "echo" and "\$$t" (set equal to $table_cell
这个过程的详细解释可以在《advanced Bash-Scripting Guide》中找到:
“Indirect referencing in Bash is a multi-step process. First, take the name of a variable: varname. Then,
reference it: $varname. Then, reference the reference: $$varname. Then, escape the first $:
\$$varname. Finally, force a reevaluation of the expression and assign it: eval newvar=\$$varname.”。
${!a}
对于\$${a},如果间接引用的那个变量的值发生了改变,则第一个变量需要被正确的解引用,所以在bash version 2 中引入了${!a},这个${!a}更加的直观,不需要eval执行。
具体在《advanced Bash-Scripting Guide》有描述:
This method of indirect referencing is a bit tricky. If the second order variable changes its value,
then the first order variable must be properly dereferenced (as in the above example).
Fortunately, the ${!variable} notation introduced with version 2 of Bash makes indirect referencing more intuitive.
t=table_cell_3
table_cell_3=24
echo "t = ${!t}" # t = 24
table_cell_3=387
echo "Value of t changed to ${!t}" # 387
# No 'eval' necessary.
# This is useful for referencing members of an array or table,
#+ or for simulating a multi-dimensional array.
# An indexing option (analogous to pointer arithmetic)
#+ would have been nice. Sigh.
exit 0
总的来说,${!a}较\$${a}更方便。