[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
- Re: Changing the way bash expands associative array subscripts, (continued)
- Re: Changing the way bash expands associative array subscripts, Alex fxmbsw7 Ratchev, 2021/03/16
- Re: Changing the way bash expands associative array subscripts, Chet Ramey, 2021/03/29
- Re: Changing the way bash expands associative array subscripts, Robert Elz, 2021/03/16
- Re: Changing the way bash expands associative array subscripts, Alex fxmbsw7 Ratchev, 2021/03/17
- Re: Changing the way bash expands associative array subscripts, Robert Elz, 2021/03/17
Re: Changing the way bash expands associative array subscripts, Chet Ramey, 2021/03/29
Re: Changing the way bash expands associative array subscripts, Koichi Murase, 2021/03/15
Re: Changing the way bash expands associative array subscripts, Jesse Hathaway, 2021/03/17
Re: Changing the way bash expands associative array subscripts,
konsolebox <=