Sample Header Ad - 728x90

Why can't I have a single quote in a bash associative array index when testing with `test -v`

2 votes
1 answer
65 views
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 ">"
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).
Asked by Harald (1030 rep)
Apr 24, 2025, 12:43 PM
Last activity: Apr 24, 2025, 05:29 PM