help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] indirection for arrays


From: Dan Douglas
Subject: Re: [Help-bash] indirection for arrays
Date: Wed, 09 May 2012 08:11:22 -0500
User-agent: KMail/4.8.3 (Linux/3.3.4-pf+; KDE/4.8.3; x86_64; ; )

On Wednesday, May 09, 2012 08:26:22 AM Greg Wooledge wrote:
> On Wed, May 09, 2012 at 02:42:34AM -0500, Dan Douglas wrote:
> >  ~ $ x=y; y=(1 2 3); declare -a "$x"+=(a b c); declare -p y
> > 
> > -bash: syntax error near unexpected token `('
> 
> I'm not sure what this was trying to demonstrate.  That the syntax does
> not work?

I explained it in a previous (long) message in this thread. Basically yes.
Declare (and related builtins) have two modes of operation - one of which
behaves like eval and performs two passes of expansion. That happens whenever
declare both has the -a flag, and doesn't recognize its argument as an array
assignment, in which case arguments get expanded like an ordinary builtin, and
then declare itself tries to re-evaluate the result. So to make it work you'd
have to use: declare -a "$x"'+=(a b c)'. It's the VERY same effect that enables
the declare -a 'keys=("${!'"$1"'}")' trick.

> > And it isn't limited to declare - eval does it too:
> (Does what?)
> 
> >  $ eval x=(1 2 3); declare -p x
> > 
> > declare -a x='([0]="1" [1]="2" [2]="3")'
> 

The above is the reverse case - it SHOULD fail with the same error as the first
declare example, because the opening paren is neither quoted nor escaped (and it
does fail in ksh93). Therefore eval is acting like a keyword just like declare
and snooping on its arguments at parse-time. I somewhat understand why declare
does this, but for eval it doesn't make sense.
-- 
Dan Douglas

Attachment: signature.asc
Description: This is a digitally signed message part.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]