I have the variable $foo="something" and would like to use:
bar="foo"; echo $($bar)
to get "something" echoed.
Join Stack Overflow to learn, share knowledge, and build your career.
|
|
In bash, you can use ${!variable} to use variable variables.
|
|||||||||||||||||||||
|
|
The accepted answer is great. However, @Edison asked how to do the same for arrays. The trick is that you want your variable holding the "[@]", so that the array is expanded with the "!". Check out this function to dump variables:
This outputs:
When given as just |
|||||||||
|
|
|
|||||||||
|
|
To make it more clear how to do it with arrays:
|
|||
|
|