Showing results for 
Search instead for 
Do you mean 

Command Substitution

SOLVED
Go to Solution
Highlighted
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
1 ACCEPTED SOLUTIONS
Acclaimed Contributor Acclaimed Contributor

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

5 REPLIES
Acclaimed Contributor Acclaimed Contributor

Re: Command Substitution

Hi:

Do you mean:

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

Regards!

...JRF...
Honored Contributor Honored Contributor

Re: Command Substitution

Try this:

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


regards,
ivan
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.
Acclaimed Contributor Acclaimed Contributor

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

Re: Command Substitution

Hi JRF,

Incredible, Thanks for the solution.

Many Thanks,
Regards,
Raghu.