bug-bash
[Top][All Lists]
Advanced

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

Re: Behaviour of test -v with assoc array and quote character in key


From: Greg Wooledge
Subject: Re: Behaviour of test -v with assoc array and quote character in key
Date: Mon, 15 Feb 2021 08:48:15 -0500
User-agent: Mutt/1.10.1 (2018-07-13)

On Mon, Feb 15, 2021 at 12:27:42AM +0100, Daniel Gröber wrote:
> I've found what I belive to be a bug in how `test -v` expands the key in an
> associative array. The following minimal test case demonstrates the
> problem:
> 
>     declare -A array
>     
>     mytest () {
>             array["$1"]=123
>             test -v array["$1"]
>             printf 'test -v array[%s]; $?=%s\n' "$1" "$?"
>     }

Do it this way instead:

unicorn:~$ mytest() { array[$1]=123; test -v 'array[$1]'; echo "$?"; }
unicorn:~$ declare -A array
unicorn:~$ mytest a
0
unicorn:~$ mytest \"
0
unicorn:~$ declare -p array
declare -A array=(["\""]="123" [a]="123" )

Yours expands $1 first, then passes array["] as an argument to test,
which then performs a *second* round of expansion/parsing.

You might also want to look at the assoc_expand_once option, although
single-quoting will work in a wider range of bash versions.




reply via email to

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