[Top][All Lists]

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

Re: Changing the way bash expands associative array subscripts

From: Koichi Murase
Subject: Re: Changing the way bash expands associative array subscripts
Date: Wed, 7 Apr 2021 01:46:27 +0900

2021年4月7日(水) 0:53 Greg Wooledge <greg@wooledge.org>:
> On Tue, Apr 06, 2021 at 06:34:21PM +0300, Ilkka Virta wrote:
> > $ declare -A a=([foo]=123 [bar]=456)
> > $ unset 'a[@]'
> > $ declare -p a
> > bash: declare: a: not found
> >
> >  i.e. both remove the whole array, not just the contents.
> Huh, so it does.  I didn't know that.
> In that case, I have no qualms about proposing that unset 'a[@]' and
> unset 'a[*]' be changed to remove only the array element whose key is
> '@' or '*', respectively, and screw backward compatibility.  The current
> behavior is pointless and is nothing but a surprise landmine.  Anyone
> relying on the current behavior will just have to change their script.

How about the cases with `test -v a[@]' and `key=@; test -v a[$key]'?

I remember there was some discussion in the bug-bash list on the
behavior of `test -v a[@]' which actually tests whether the array `a'
has at least one element. It cannot be replaced by `test -v a' because
`a' implies `a[0]' here. But if I correctly understand it, `test -v
a[@]' can be replaced by `((${#a[@]}))'. Should any scripts relying on
`test -v a[@]' to test the array elements also be changed?

Looking at another thread
I'm now also interested in how we can handle the indirect expansions
for 'a[@]' and the namerefs for 'a[@]':

$ declare -A a=(['@']=x [1]=y)
$ # indirect expansions
$ iref1=a[@]; printf '<%s>' "${!iref1}"; echo
$ key=@; iref2=a[$key]; printf '<%s>' "${!iref2}"; echo
<y><x>    # <-- unexpected
$ # namrefs
$ declare -n nref1=a[@]; printf '<%s>' "$nref1"; echo
<y x>
$ key=@; declare -n nref2=a[$key]; printf '<%s>' "$nref2"; echo
<y x>    # <-- unexpected

This is harder to solve because these variables just contain the
literal string of the form `name[key]' so the syntactic information of
`ref=a[@]' or `ref=a[$key]' is already lost, but there still seems to
be a certain demand for expanding multiple elements through the
indirect expansions and the nameref variables.


reply via email to

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