1748223 Members
4524 Online
108759 Solutions
New Discussion юеВ

Re: Command Substitution

 
SOLVED
Go to solution
Raghu Chikkamenahalli
Frequent Advisor

Command Substitution

Hi All,

Is nested substitution is possible in shell scripts.
For example
VAR=/home/raghul
VAR1=VAR

And my requirement is, to get the /home/raghul using VAR1 variable.
i.e
If I use ${VAR1} will give VAR and it needs to be get expanded to /home/raghul in one command using echo.

Kindly help if it can be possible.

Thanks,
Regards,
Raghu
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor

Re: Command Substitution

Hi:

Do you mean:

VAR=/home/raghul
# VAR1=${VAR}
# echo $VAR1
/home/raghul

Regards!

...JRF...
Ivan Krastev
Honored Contributor

Re: Command Substitution

Try this:

VAR=/home/raghul
VAR1=$VAR
echo $VAR1


regards,
ivan
Raghu Chikkamenahalli
Frequent Advisor

Re: Command Substitution

Tried this option VAR1=${VAR1} before this post.
let us say, script check.sh

cat check.sh
------------------------------------------
HOME_PATH="/home/raghul"

VAR=${1}

echo ${VAR}

------------------------------------------
Now If I execute the program
$./check.sh HOME_PATH,
The echo statement should display /home/raghul.
===> ${${VAR}}
===> ${HOME_PATH}
===> /home/raghul
I am extremly sorry if my first question is not clear or deviated from my requirement. The requirement is explained in the second one.

Kindly guide me.

Many Thanks,
Regards,
Raghu.
James R. Ferguson
Acclaimed Contributor
Solution

Re: Command Substitution

Hi (again) Raghu:

Try this:

# cat .check.sh
#!/usr/bin/sh
HOME_PATH="/home/raghul"
VAR=\${$1}
eval echo ${VAR}

# .check.sh HOME_PATH
/home/raghul

Regards!

...JRF...

Raghu Chikkamenahalli
Frequent Advisor

Re: Command Substitution

Hi JRF,

Incredible, Thanks for the solution.

Many Thanks,
Regards,
Raghu.