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.

2 Responses to “How to get the value of a $v2, the name of which is stored in $v1”

  1. Artem Nosulchik Says:

    “BASH Cures Cancer” rocks! I was looking for this tip and at last I found it here, thanks!

  2. Chris F.A. Johnson Says:

    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.

Leave a Reply

If Wordpress eats your comment (shell output, loops, ex..) email the text to me.