bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: can't unset hash item with specific key


From: Andreas Kusalananda Kähäri
Subject: Re: can't unset hash item with specific key
Date: Sun, 12 Dec 2021 23:46:24 +0100

On Sun, Dec 12, 2021 at 08:47:21PM +0100, l.bashbug@scarydevilmonastery.net 
wrote:
> Configuration Information [Automatically generated, do not change]:
> Machine: x86_64
> OS: linux-gnu
> Compiler: gcc
> Compilation CFLAGS: -g -O2 -fstack-protector-strong -Wformat 
> -Werror=format-security -Wall 
> uname output: Linux h2 5.15.0-2-amd64 #1 SMP Debian 5.15.5-1 (2021-11-26) 
> x86_64 GNU/Linux
> Machine Type: x86_64-pc-linux-gnu
> 
> Bash Version: 5.1
> Patch Level: 12
> Release Status: release
> 
> Description:
>       making left square bracket char ([) part of the key into a hash causes
>       unset to silently fail when trying to unset that specific hash item.
>       The position of left square bracket char in the string used as key 
> doesn't
>       matter. unset also fails when the key is a single [ character.
>       Please refer to the following script which attempts to demonstrate this 
> quirk.
> 
> Repeat-By:
> 
> # diagnostic shows remains of hash after attempts to unset single item.
> show()  { echo "$1, ${#a[@]} items in a: a['${!a[@]}']=${a['[foo]']}"; }
> 
> 
> # prepare test scenario
> 
> declare -A a                          # make "a" a hash
> a['[foo]']="bar"                      # assign "bar" to item keyed [foo]
> show "good"                           # correctly output item, show correct 
> item count
> 
> 
> # intention is to get rid of the item again, by unsetting the single item, 
> not the whole array.
> # several ways of quoting are attempted here
> 
> unset "a['[foo]']"                    # try to remove item
> show "wrong"                          # can still output item, item still 
> counts
> 
> unset 'a["[foo]"]'                    # try to remove item
> show "wrong"                          # can still output item, item still 
> counts
> 
> unset 'a[[foo]]'                      # try to remove item
> show "wrong"                          # can still output item, item still 
> counts
> 
> unset "a[[foo]]"                      # try to remove item
> show "wrong"                          # can still output item, item still 
> counts
> 
> unset "a[\[foo]]"                     # try to remove item
> show "wrong"                          # can still output item, item still 
> counts
> 
> unset "a["\[foo]"]"                   # try to remove item
> show "wrong"                          # can still output item, item still 
> counts
> 
> unset "a['['foo]]"                    # try to remove item
> show "wrong"                          # can still output item, item still 
> counts
> 
> a["[foo]"]=""                         # Best I can currently do
> show "set empty, item still counted"  # item still there though contents are 
> empty string.

Just to say this unsets the element correctly:

        k='[foo]'
        unset -v 'a["$k"]'

-- 
Andreas (Kusalananda) Kähäri
SciLifeLab, NBIS, ICM
Uppsala University, Sweden

.



reply via email to

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