I am writing a shell script (#!/bin/sh) which has a variable VAR which contains the name of another variable FOO, which in turn is set to BAR.
FOO=BAR
VAR=FOO
I want to get the value of the variable named in VAR, so something like:
echo "${$VAR}"
But that does not seem to work. Suggestions?
In Bash:
echo "${!VAR}"
Without Bash (though it works in Bash too):
eval echo "\${$VAR}"
Beware: eval
is a very general mechanism that can run you into problems very easily. It works fine here, but be cautious about using it more generally.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With