Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Build a variable name from another variable?



Omen Wild wrote:



I am not totally sure what it is called, but I want to build a variable
name dynamically, from the contents of another variable.  That is, I
have:

----- Begin script -----
barcontents="bar contents"
baz="bar"

echo $barcontents # <---- effect I want
echo ${${baz}contents} # <---- how I want to get there
----- End script -----

I get this error: bad substitution

I think the answer involves the (P) flag, as in:
echo ${${(P)baz}contents}

Back in the old days, before these fancy complicated shells, I'd just do this:

eval echo \$${baz}contents

;-)




Cosmo



Messages sorted by: Reverse Date, Date, Thread, Author