[ale] variable variables in bash/ksh?

Geoffrey esoteric at 3times25.net
Mon Apr 19 11:30:46 EDT 2004


John Wells wrote:
> Guys,
> 
> I'm trying to figure out a way to use, to borrow from PHP, variable
> variables in shell.
> 
> For example, consider the following:
> 
> XYZ="ABC" ABC=123456
> 
> # in php, I can do the following and get "123456" as the result, so
> it # first evals XYZ to get ABC, and then applies the second $ to ABC
> to # get the value of $ABC echo $$XYZ
> 
> Anyone know how to do this in bash or ksh?  Seems like something I
> once knew, but it escapes me now...

I think the easiest way to do this in ksh is:

${XYZ:-$ABC}

If XYZ is not set, then the value of ABC is evaluated and returned.  As 
long as XYZ is never set to anything, this construct will always 
re-evaluate ABC and return that value.

This may or may not work for you depending ont the context in which 
you'll use it.

I don't know if that works in bash, to tired to try... :)

-- 
Until later, Geoffrey                     Registered Linux User #108567
Building secure systems in spite of Microsoft



More information about the Ale mailing list