bug-bash
[Top][All Lists]
Advanced

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

Re: quoted compound array assignment deprecated


From: Stephane Chazelas
Subject: Re: quoted compound array assignment deprecated
Date: Tue, 18 Aug 2015 21:21:25 +0100
User-agent: Mutt/1.5.21 (2010-09-15)

2015-08-17 10:19:00 +0200, isabella parakiss:
> Quoting is necessary in a few cases:
> 
> $ var=foo; declare -A "arr$var=([x]=y)"
> bash: warning: arrfoo=([x]=y): quoted compound array assignment deprecated
> $ var=foo; declare -A arr$var=([x]=y)
> bash: syntax error near unexpected token `('
> $ var=foo; declare -A "arr$var"=([x]=y)
> bash: syntax error near unexpected token `('
> 
> I don't think this should be the default behaiour...
[...]

This typically requires two levels of evaluation. The syntax of
"declare" is now more consistent with that of bare assignments
and there are fewer cases where "declare" ends up evaluating
code that it's not meant to.

Here, I'd do:

declare -A "arr$var"

eval "arr$var"'=([x]=y)'


By using "eval" (which has the reputation of being dangerous),
you're making it clear that there is that second level of
evaluation that one should be careful around (and which is there
as well with declare but less obvious).

The way you're expecting declare to work is just a disguised
eval, it's not any safer than eval. To me, variable declaration
should be separate from evaluating code. Ideally, I'd rather
"declare" didn't do assignments either (note that it was ksh
breaking "export" by allowing assignments and causing confusions
between simple commands and assignments that was not there in
the Bourne shell).

-- 
Stephane



reply via email to

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