On Fri, Oct 28, 2022 at 06:22:22PM -0700, Corey Hickey wrote:
I'm using '@' for the variable as a deliberately pathological value.
With bash 5.2 defaults, I can't find a way to make this work. Keeping
the subscript variable escaped results in an evaluation to an undefined
key (apparently):
$ ./bash -c 'declare -A assoc ; var=@ ; assoc[$var]=1 ; echo "$((1 +
assoc[\$var]))"'
1
The [\$key] workaround that works in previous versions of bash does not
work in 5.2.
https://mywiki.wooledge.org/BashPitfalls#pf62
As of bash 5.2, the only safe, working way to modify an element of an
associative array in a calculation is to make a temporary copy of the
value in a regular (string) variable.
tmp=${hash[$key]}
((tmp++)) # Safe.
hash[$key]=$tmp
# Or:
tmp=${hash[$key]}
hash[$key]=$((tmp+1)) # Safe.
I believe you're hitting the same issue here, even though you're only
trying to retrieve an associative array element in a math context,
rather than trying to modify one.