[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Unsetting members of associative arrays
From: |
Jesse Hathaway |
Subject: |
Re: [Help-bash] Unsetting members of associative arrays |
Date: |
Thu, 11 Apr 2019 12:42:58 -0500 |
On Thu, Apr 11, 2019 at 12:35 PM Douglas Lewan <address@hidden> wrote:
>
> The following code doesn't do what I expect. The ostensibly unset array
> element is still there. There's a work-around: Keep track of the indexes
> to unset and do that outside the loop.
>
> declare -A array
>
> for i in address@hidden ; do
> do
> if [[ ${array[${i}]} = bob ]]
> then
> unset array[${i}]
> fi
> done
>
> Is there a real reason for that or is it a bug?
That code works for me on Bash 5.0.3, or am I misunderstanding
your question?
#!/bin/bash
set -o nounset
set -o errexit
set -o pipefail
declare -A array=( ['butter']='bob' )
for i in "address@hidden"; do
if [[ ${array[${i}]} == bob ]]; then
unset array["${i}"]
fi
done
declare -p array
> bash -x ~/test.sh
+ set -o nounset
+ set -o errexit
+ set -o pipefail
+ array=(['butter']='bob')
+ declare -A array
+ for i in "address@hidden"
+ [[ bob == bob ]]
+ unset 'array[butter]'
+ declare -p array
declare -A array=()