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: konsolebox
Subject: Re: Changing the way bash expands associative array subscripts
Date: Wed, 31 Mar 2021 07:05:29 +0800

On Tue, Mar 16, 2021 at 8:12 AM Chet Ramey <chet.ramey@case.edu> wrote:
> This means that, given the following script,
>
> declare -A a
> key='$(echo foo)'
> a[$key]=1
> a['$key']=2
> a["foo"]=3
>
> # never worked
> unset -v a[$key]
> declare -p a
>
> # unsets element with key $key
> unset -v a['$key']
> declare -p a
>
> # unsets element with key $(echo foo)
> unset -v a["$key"]
> declare -p a
>
> # unsets element with key foo
> eval unset -v a\["$key"\]
> declare -p a
>
> you'll get this output:
>
> example: line 8: unset: `a[$(echo': not a valid identifier
> example: line 8: unset: `foo)]': not a valid identifier
> declare -A a=(["\$(echo foo)"]="1" ["\$key"]="2" [foo]="3" )
> declare -A a=(["\$(echo foo)"]="1" [foo]="3" )
> declare -A a=([foo]="3" )
> declare -A a=()

As I've observed, in single expansion mode, unset will fail to unset a
value when the key has a closing bracket in it. Perhaps unset should
check the last character first when looking for the closing bracket.
Tested in 5.1.4.

-- 
konsolebox



reply via email to

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