Using a variable's value as a variable name


To use the value stored in a shell variable as the name for another variable, use this syntax:

x=string
export $(echo $x)=hello

This will result in

echo $string
hello

Here is another approach. Assume x is already assigned the value 'string' and string is assigned the value 'hello'.

eval value=\$$x
echo $value
hello

This is useful, say, in a for loop, where you don't know what the value of the assigned to the first variable will be. See this for a discussion about this and some other syntax.

11/14/2006