On 7/1/25 2:50 PM, Zachary Santer wrote:

On the other hand, there's no expectation that this should work:
$ op='||'
$ [[ ${var_is_unset} == 'true' ${op} ${var} == 'foo' ]]
and it doesn't, whereas
$ var_is_unset=1
$ unset var
$ op='||'
$ (( var_is_unset ${op} var > 0 ))
does currently work, 'set -u' and all.

The arithmetic expression parser isn't part of the shell parser per se;
it's a parse-and-evaluate-on-the-fly parser that is only invoked when
needed.

In this case, the initial expansion step turns this expression into

var_is_unset || var > 0

which gets handed to the arithmetic evaluator. It evaluates the LHS of
the || operator, which is enough to cause it to skip evaluating the RHS,
as you'd expect.

This means that arithmetic syntax errors are detected when the expression
is evaluated, not when the (( command is parsed. This is due to its origins
as syntactic sugar for `let "expression".

The [[ conditional command, on the other hand, is parsed as part of the
shell parser. This means that syntax errors like ${op} are detected at
parse time.

--
``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/

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature

Reply via email to