When I run the following code
declare -A X # get an associative array
index="a'b" # a somewhat weird index
X[$index]="this is set"
echo "<<${X[$index]}>>"
if test -v X[$index]; then # behaves in an unexpected way
echo indeed, this is set
else
echo no, it is not
fi
the result is no, it is not
in my bash version 5.1.16(1). As soon as i remove the single quote from the index
, it works as expected, telling that indeed, this is set
.
From the bash manual:
Indexed arrays are referenced using integers [...]; associative arrays use arbitrary strings.
Given that I can properly use the index
to set and retrieve the value, I wonder if this is a bug in test -v
or if there is some explanation why this does not work or whether different quoting can get test -v
to work. (I could use test -z
in this particular case, but the question is about -v
).
test -v 'X[$index]'
. It looks weird, but that's what you'll use. A real answer would explain why.bash
, this I would have felt stupid to even try this. This looks liketest -v
does its own parsing of stuff, a hint about which seems missing in the documentation.