bug-bash
[Top][All Lists]
Advanced

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

Re: Changing the way bash expands associative array subscripts


From: Chet Ramey
Subject: Re: Changing the way bash expands associative array subscripts
Date: Mon, 12 Apr 2021 11:52:50 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.9.0

On 4/9/21 12:20 PM, Koichi Murase wrote:

That said, the fact that you can put 'a[@]' in an indirect variable and
get an array expansion out of "${!x}" is completely repulsive to me.

Currently, we need to write as «iref='a[$key]'; echo "${!iref}"» so
that $key is not expanded until the referencing (just like «unset
'a[$key]'»). If the new assoc_expand_once enables the indirect
expansions of the form «iref="d[$key]"; echo "${!iref}"» like «unset
"a[$key]"» and also if we don't change the current behavior of
«iref=a[@]; echo "${!iref}"» (being expanded to all the elements of
the array) as Greg's suggestion, we need to work around the case key=@
for «iref="d[$key]"; echo "${!iref}"». However, it seems to me that
there is no workaround. What string should we assign to `iref' to
create a reference to the element associated with key=@ with Greg's
suggestion?

Ah, ok. I see. You can assign '\@' to iref to get the behavior you want.
It's not perfect, but it's possible:

$ cat x6b
shopt -s assoc_expand_once
declare -A a=(['@']=x [1]=y)

printf '<%s>' "${a[@]}"; echo

printf '<%s>' "${a[\@]}"; echo

iref1='a[\@]'
echo "$iref1"
printf '<%s>' "${!iref1}"; echo

key='\@'; iref2=a[$key]
echo "$iref2"

printf '<%s>' "${!iref2}" ; echo
$ ./bash ./x6b
<y><x>
<x>
a[\@]
<x>
a[\@]
<x>


Once you choose to have ${a[@]} expand to all the elements of an array,
whether it's indexed or associative -- and that choice came years ago --
using that character as a key is always going to require a workaround.
Some of them won't be pretty.

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/



reply via email to

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