Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

how to evaluate this variable

SOLVED
Go to Solution
Regular Advisor

how to evaluate this variable

say if I have

fred=/tmp
fred2=fred

how do I get the variable for fred from fred2

if I do echo "$fred2", it doesnt work..

I tried many ways, none of that work, please advise...


1 ACCEPTED SOLUTION
Highlighted
Honored Contributor Honored Contributor

Re: how to evaluate this variable

Try using 'eval'. Here is one way to do it:

eval echo \$$FRED2
/tmp


JP
5 REPLIES
Regular Advisor

Re: how to evaluate this variable

posted too fast...I meant to say. I want to get the value of fred from fred2...

like "echo $fred2" will print "/tmp"

I know "echo $fred2" is wrong..."echo $$fred2" is also wrong :-(
Highlighted
Honored Contributor Honored Contributor

Re: how to evaluate this variable

Try using 'eval'. Here is one way to do it:

eval echo \$$FRED2
/tmp


JP
Valued Contributor Valued Contributor

Re: how to evaluate this variable

If you want to set fred2 to the value of fred, try this...

fred=/tmp
fred2=$fred

KJ
No matter where you go...there you are.
Honored Contributor Honored Contributor

Re: how to evaluate this variable

Oops. I used upper case and you used lower:

eval echo \$$fred2
/tmp

Regular Advisor

Re: how to evaluate this variable

such easy answer..I was so close..

I used

`eval echo \$$i`

I should not put ` `, it will make it evalute 3 times :-(

thank you!!

I got it