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

Re: Bad expansion

On Jan 21,  8:40pm, DervishD wrote:
> >>    # Bad substitution
> >>    echo ${${testing[1]}_VALUE}
> >echo ${(e):-\$${testing[1]}_VALUE}
> >The :-\ is a smiley that means "maybe there ought to be a neater way".
>     Are you joking?

Yes, he is.

>     "Expand <NULL> identifier, and, if it is not defined or missing
> (which is true), expand the other thing, that is, \$${testing[1]}_VALUE"

Almost.  You forgot about the (e).  The whole expression means:

Expand the unnamed identifier, and, if it is not defined or has an empty
value (which is true), insert \$${testing[1]}_VALUE instead; then perform
parameter expansion, command substitution and arithmetic expansion on the

"The result" is of course a `$' (from `\$') followed by the value of the
${testing[1]} expansion, followed by `_VALUE'.  So if ${testing[1]} is,
for example, `foo', then "the result" is `$foo_VALUE', which is parameter-
expanded because of (e), and gives you what you want.

Using (P) instead of (e) allows you to leave off the `\$', because (P)
means "interpret the result as a parameter name and use the value of
that parameter."  However, with (P), you have to use an extra level of
${...}, because (P) takes effect at a different time in the expansion
process than (e) does.

On Jan 21,  8:46pm, DervishD wrote:
>     Seriously: why am I having the 'bad substitution' error? What am
> I doing wrong?. This interest me more than the solution.

Anything that looks like ${${...}} is called a "nested substitution".  The
stuff inside the outermost ${...} can take one of two forms:

(1) it can be the name of a parameter (in which case the substitution is
    not nested, of course), plus an optional [...] subscript;
(2) it can be another ${...} substitution expression plus an optional

That's all; nothing else.  In ${${testing[1]}_VALUE}, the `_VALUE' part
is neither part of the inner ${...} substitution, nor is it a subscript,
so it can't be there.  In ${:-${testing[1]}_VALUE}, the `_VALUE' becomes
part of the outer ${...}, so then it's OK.

Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   

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