Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Indirect reference to another variable 1

Status
Not open for further replies.

cptk

Technical User
Mar 18, 2003
305
US
Using ksh shell ...

> dog="new tricks"
> x="dog"

How do I indirectly reference the variable "dog" via "x"?

e.g.
> echo ${$x} ## I want to display "new tricks"

PS - I can't use typeset -n


I know I've done this before. Any help to jog my memory w/b appreciated!
 

Check out the eval function. [3eyes]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
or:

value=$(eval echo \$$x)
echo $value

amounts to the same thing I believe, but less ugly...


HTH,

p5wizard
 
Yep, that works ...

> eval echo $`eval echo ${x}`

I was trying to use parenthesis instead of the back tics.
For some reason, only the back tics work for this scenario.

e.g. -
The following produce the same output:
echo "XYZ$(pwd)"
echo "XYZ`pwd`

...thanks!
 
p5Wizard ... I like that the best !!!

echo $(eval echo \$$x)
 
Hi

Nice one, p5wizard. Now I a key difference between executing with backticks and parenthesis. I tried something similar too, but only with backticks. I think is time to change some habits...

Feherke.
 
[tt]bash[/tt] supports a special syntax for indirection:
Code:
echo ${!x}

The [tt]ksh[/tt] man page suggests that it might work there, too, but I can't really tell.
 
chipperMDW -
Yes, this is nicer, but unfortunately this doesn't work in ksh shell in solaris 9.
 
Simpler as...
Code:
eval echo \$$x
or
Code:
eval value=\$$x
print $value
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top