On Tue, Mar 16, 2021 at 8:12 AM Chet Ramey <chet.ra...@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