On 2/23/21 10:55 AM, Greg Wooledge wrote:
Oğuz (oguzismailuy...@gmail.com) wrote:
`(( assoc[\$var]++ ))' works fine as usual.

unicorn:~$ bash-5.1
unicorn:~$ declare -A hash
unicorn:~$ key=\'\]
unicorn:~$ hash[$key]=17
unicorn:~$ (( hash[\$key]++ ))
unicorn:~$ declare -p hash
declare -A hash=(["']"]="18" )
unicorn:~$ (( 'hash[$key]'++ ))
bash-5.1: ((: 'hash[']]'++ : syntax error: operand expected (error token is 
"'hash[']]'++ ")
unicorn:~$ (( hash['$key']++ ))
bash-5.1: ((: hash['']']++ : syntax error: invalid arithmetic operator (error token is 
"']++ ")

You're so close to getting it.

Pay attention to the error message. The contents of (( are expanded as if
they are between double quotes (as documented, and like $((expression)) ).
Single quotes aren't special in double quotes, so they don't matter and are
preserved in the expression that gets passed to the evaluator, which is as
in the error message (`hash['']']++'). The closing bracket is ambiguous.

In retrospect, the issue is having the expression evaluator treat the array
subscript as somehting to be expanded, but that was the consequence of the
decision to treat subscripts consistently across different contexts.

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    c...@case.edu    http://tiswww.cwru.edu/~chet/

Reply via email to