#!/bin/bash # ind-ref.sh: Indirect variable referencing. # Accessing the contents of the contents of a variable. 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 echo # Now, let's try changing the second-order reference. 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_3) echo # (Thanks, Stephane Chazelas, for clearing up the above behavior.) # Another method is the ${!t} notation, discussed in "Bash, version 2" section. # See also ex78.sh. exit 0