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

Re: expansion of nested parameters



Phil Pennock wrote:
>a=foo
>wib_foo_ble=Wow
>c=\$wib_${a}_ble
>print ${(e)c}

>1: Why does the second print statement print out the current process ID
>   from $$ instead of printing the same as the first print statement?

Because you put "$" where it was expecting a parameter name, and "$"
is in fact a valid parameter name.

>2: Is there a way to do this without using an intermediate variable, as
>   I've done with $c and without using eval statements?

Yes.  You can use the form "${:-arbitrary string}" to get a $ expansion
that expands to an arbitrary string.  $ expansions can be used in
the string.  So you want to do

	a=foo
	wib_foo_ble=Wow
	print ${(e):-\$wib_${a}_ble}

This trick is now documented, in zshexpn(1):

#       ${name:-word}
#              If  name is set and is non-null then substitute its
#              value; otherwise substitute word. If name is  miss-
#              ing, substitute word.

-zefram



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