[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Destroying arbitrary subset in an associative array?
From: |
Greg Wooledge |
Subject: |
Re: [Help-bash] Destroying arbitrary subset in an associative array? |
Date: |
Mon, 19 Sep 2016 09:25:56 -0400 |
User-agent: |
Mutt/1.4.2.3i |
On Sun, Sep 18, 2016 at 03:10:10PM -0400, Mingye Wang (Arthur2e5) wrote:
> a valid identifier". I am using bash 4.3.42.
>
> declare -A dict
> dict['"']=1
> dict['`']=2
> dict["'"]=3
> dict['\']=4
> declare -p dict
>
> del(){ unset -v dict["$1"]; }
> for k in "address@hidden"; do del "$k"; done
Dan's response was rather cryptic, but here's the informative part buried
deep inside it:
imadev:~$ bash-4.3
imadev:~$ declare -A dict
imadev:~$ dict['"']=1 dict['`']=2 dict["'"]=3 dict['\']=4
imadev:~$ dictdel() { unset -v 'dict[$1]'; }
imadev:~$ for k in "address@hidden"; do dictdel "$k"; done
imadev:~$ declare -p dict
declare -A dict='()'
Rule of thumb: never write unset foo[x]. Always write unset 'foo[x]'.
The simplest reason for this is that foo[x] is a valid glob (filename
expansion), and will be replaced by foox if there happens to be a file
by that name in $PWD.
The more complex reason is... whatever Dan said.