bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Behaviour of test -v with assoc array and quote character in key


From: Chet Ramey
Subject: Re: Behaviour of test -v with assoc array and quote character in key
Date: Tue, 23 Feb 2021 17:34:02 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.7.1

On 2/23/21 10:55 AM, Greg Wooledge wrote:
Oğuz (oguzismailuysal@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    chet@case.edu    http://tiswww.cwru.edu/~chet/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]