Re: Command Substitution (146 Views)
Reply
Frequent Advisor
Raghu Chikkamenahalli
Posts: 90
Registered: ‎12-02-2003
Message 1 of 6 (146 Views)
Accepted Solution

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
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 6 (146 Views)

Re: Command Substitution

Hi:

Do you mean:

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

Regards!

...JRF...
Honored Contributor
Ivan Krastev
Posts: 2,156
Registered: ‎06-25-2006
Message 3 of 6 (146 Views)

Re: Command Substitution

Try this:

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


regards,
ivan
Frequent Advisor
Raghu Chikkamenahalli
Posts: 90
Registered: ‎12-02-2003
Message 4 of 6 (146 Views)

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.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 6 (146 Views)

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...

Frequent Advisor
Raghu Chikkamenahalli
Posts: 90
Registered: ‎12-02-2003
Message 6 of 6 (146 Views)

Re: Command Substitution

Hi JRF,

Incredible, Thanks for the solution.

Many Thanks,
Regards,
Raghu.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.