From: "Bart Schaefer" <schaefer@brasslantern.com>
To: DervishD <raul@viadomus.com>
Cc: zsh-workers@sunsite.dk
Subject: Re: Bad expansion
Date: Mon, 21 Jan 2002 13:31:16 -0800 [thread overview]
Message-ID: <020121133116.ZM27648@candle.brasslantern.com> (raw)
In-Reply-To: <E16SkJ9-0001G9-00@DervishD.viadomus.com>
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
result.
"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
subscript.
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
next prev parent reply other threads:[~2002-01-21 21:31 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-01-21 19:40 DervishD
2002-01-21 19:46 ` DervishD
2002-01-21 21:31 ` Bart Schaefer [this message]
-- strict thread matches above, loose matches on Subject: below --
2002-01-22 0:17 DervishD
2002-01-21 14:56 DervishD
2002-01-21 14:51 ` Zefram
2002-01-21 17:12 ` Bart Schaefer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=020121133116.ZM27648@candle.brasslantern.com \
--to=schaefer@brasslantern.com \
--cc=raul@viadomus.com \
--cc=zsh-workers@sunsite.dk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).