Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: bad math expression: illegal character: "



> On Jun 18, 2020, at 7:19 AM, Mikael Magnusson <mikachu@xxxxxxxxx> wrote:
> 
>> On 6/18/20, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
>> 
>>> On Fri, Jun 5, 2020 at 2:02 PM Lawrence Velázquez <vq@xxxxxxxxx> wrote:
>>> 
>>>> On Jun 5, 2020, at 2:18 PM, Artur Renault <Artur.Renault@xxxxxxxxxxxxx>
>>>> 
>>>> while (( "$#" )); do
>>>> 
>>>> repro.sh:1: bad math expression: illegal character: "
>>> 
>>> For what it's worth, dash also rejects this. I assume that it and
>>> zsh are just more strict about what constitutes a math expression.
>> 
>> Does anybody know if this is something we should change for sh
>> emulation, or is it merely another unspecified POSIX corner-case?
> 
> If this is a POSIX requirement, it must have been very specific about
> when quotes should be allowed (bash):
> 
> $ (( "0" )); echo $?
> 1
> $ echo $(( "0" ))
> bash: "0" : syntax error: operand expected (error token is ""0" ")

Looks like the bash behavior changed at some point?

    % /bin/bash --version | head -n 1
    GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
    % /bin/bash -c '(( "0" )); echo $?'
    1
    % /bin/bash -c 'echo $(( "0" ))'
    /bin/bash: "0" : syntax error: operand expected (error token is ""0" ")

    % /opt/local/bin/bash --version | head -n 1
    GNU bash, version 5.0.17(1)-release (x86_64-apple-darwin18.7.0)
    % /opt/local/bin/bash -c '(( "0" )); echo $?'
    1
    % /opt/local/bin/bash -c 'echo $(( "0" ))'
    0

Anyway, unless I'm missing something, POSIX seems pretty clear about
this, as far as $((...)) goes:

    The expression shall be treated as if it were in double-quotes,
    except that a double-quote inside the expression is not treated
    specially. The shell shall expand all tokens in the expression
    for parameter expansion, command substitution, and **quote
    removal**. [Emphasis mine.]

    Next, the shell shall treat this as an arithmetic expression
    and substitute the value of the expression.

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_04

vq


Messages sorted by: Reverse Date, Date, Thread, Author