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.