How to get the value of a $v2, the name of which is stored in $v1
October 18th, 2007
Several people have asked how to get the value of a $v2, the name of which is stored in $v1. This is in the BASH faq. Here is a short demo very similar to the FAQ’s demo:
$ v1=v2
$ v2="rose..bud...."
$ echo ${!v1}
rose..bud....
$ eval 'echo ${'$v1'}'
rose..bud....
I can confirm that the “${!v1}” form works on a few modern Linux Distro’s, AIX 5.3, and Solaris 8-10.


October 18th, 2007 at 3:21 pm
“BASH Cures Cancer” rocks! I was looking for this tip and at last I found it here, thanks!
October 26th, 2007 at 5:19 pm
The indirect expansion has been included since bash 2.0 (1996).
I usually use the portable method:
eval “printf ‘%s\n’ \${$v1}”
It works in any Bourne-type shell.