Replacing a string instead of a variable in the shell

I pass the string as an argument to the shell script. and the shell script should tell me if the passed argument is a variable

something like that

if [ ! -z ${$1} ] ; then echo yes! $1 is a variable and its value is ${$1} fi 

but that gives me the wrong err replacement ..

I definitely know that I missed something ... help me!

For example, use:

 $ myscript.sh HOME yes! HOME is a variable and its value is /home/raj 
+4
source share
2 answers

The syntax for this is:

 ${!VAR} 

Example:

 $ function hello() { echo ${!1}; } $ hello HOME /home/me 
+4
source

Found it here: http://www.linuxquestions.org/questions/programming-9/bash-how-to-get-variable-name-from-variable-274718/

All you need to do:

 if [ ! -z ${!1} ]; then echo yes $1 is a variable and its value is ${!1} fi 
+1
source

All Articles