If want to find out the value of a variable whose name is stored in another variable, you need to dereference the variable. This is how you do it.

Lets say the name of the variable is test123 and it has a value equal to 123. Also the name test123 is stored in another variable varname. To find out the value of test123, you will use the ! dereferencing operator.

@~$ test123=123
@~$ varname=test123
@~$ echo ${!varname}

Took me a while to figure this out.

There are times when you are writing a script and you have the name of a variable as a variable and the desired value as another variable. Now you want to declare the stored name as a variable and assign the stored value to it. This is how you do it.


eval ${variableName}=`echo -ne \""${variableVal}"\"`

echo $test #"test" variable created dynamically using eval

What the script does is, it creates a variable with a name same as the string(“test”) stored in $variableName, and assigns it the value equal to the one stored in $variableVal(“123”).