From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 2682 invoked from network); 21 Jan 2002 21:31:37 -0000 Received: from sunsite.dk (130.225.247.90) by ns1.primenet.com.au with SMTP; 21 Jan 2002 21:31:37 -0000 Received: (qmail 23297 invoked by alias); 21 Jan 2002 21:31:26 -0000 Mailing-List: contact zsh-workers-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 16477 Received: (qmail 23285 invoked from network); 21 Jan 2002 21:31:25 -0000 From: "Bart Schaefer" Message-Id: <020121133116.ZM27648@candle.brasslantern.com> Date: Mon, 21 Jan 2002 13:31:16 -0800 In-Reply-To: References: X-Mailer: Z-Mail Lite (5.0.0 30July97) To: DervishD Subject: Re: Bad expansion Cc: zsh-workers@sunsite.dk MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii 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 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