Storing bash output into a variable using eval

newVariable=$(eval echo \$$var)

You can try this: It worked for me on bash.

c=$(eval echo \${$var})

Example: If HOME is the environment variable that contains your home directory like /export/users/john, then embedding a variable within another variable and then unpacking that combination would work like this:

var=HOME
c=$(eval echo \${$var})
echo $c
/export/users/john

While this might look unnecessarily convoluted it will be useful in certain scenarios.


Like this:

eval c=\$$var

a better, safer way is to use indirection:

c=${!var}

This can achieved by using the back quote syntax : ` `

c=`$var`

Tags:

Bash