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

Re: minor bug with ${}



Daniel Brahneborg wrote:
> This ought to work, but doesn't:
> 
> a=b
> b=c
> echo ${a}
> echo ${${a}}
> 
> Both echos print 'b', which is wrong.

So called `nested' parameter substitution has actually always been a
bit different to what you would expect.  Instead of re-evaluating the
inner substitution as a name, it's simply there to allow you to do
more than one form of special substitution on the same variable.  For
example,

echo ${${a:s/b/cd/}#c}

substitutes b to cd then removes the c: it doesn't treat the `cd' as a
variable name.

I've always found this rather counter-intuitive, but it's been there
since one of the early patches posted to the list some five years ago.
A better syntax would have been something like `${{a:s/b/cd/}#c}' to
do the substitution above, with an extra $ doing what you were
expecting.  (It's a bit late now, since it would wreck all of Bart's
scripts, yet again :-(.)

Consequently, you need one of the usual circumlocutions with eval,
like

eval echo \${${a}}

-- 
Peter Stephenson <pws@xxxxxx>       Tel: +49 33762 77366
WWW:  http://www.ifh.de/~pws/       Fax: +49 33762 77330
Deutches Electronen-Synchrotron --- Institut fuer Hochenergiephysik Zeuthen
DESY-IfH, 15735 Zeuthen, Germany.



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