• double-quotes in arithmetic expressions (was: random case construct in

    From Helmut Waitzmann@21:1/5 to All on Wed Aug 10 16:46:20 2022
    Janis Papanagnou <[email protected]>:
    On 09.08.2022 19:09, Helmut Waitzmann wrote:
    v='6*7'
    printf '%s\n' "$((v))"
    printf '%s\n' "$((${v}))"

    I anyway wouldn't have expected that formulas in string variables
    would have been handled (like ksh/bash/zsh do) in the first place.

    A probably more interesting case (where these three shells differ)
    is
    printf '%s\n' $(("${v}"))

    POSIX (<https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_04>)
    says:

    $((expression))

    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.

    Does "a double-quote inside the expression is not treated specially"
    say that double-quotes are treated literally, i.e. excluded from
    quote removal, thus left as (a syntactic invalid) part of the
    arithmetic expression?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Helmut Waitzmann on Wed Aug 10 17:45:51 2022
    On 10.08.2022 16:46, Helmut Waitzmann wrote:
    Janis Papanagnou <[email protected]>:
    On 09.08.2022 19:09, Helmut Waitzmann wrote:
    v='6*7'
    printf '%s\n' "$((v))"
    printf '%s\n' "$((${v}))"

    I anyway wouldn't have expected that formulas in string variables
    would have been handled (like ksh/bash/zsh do) in the first place.

    A probably more interesting case (where these three shells differ) is
    printf '%s\n' $(("${v}"))

    POSIX (<https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_04>)
    says:

    $((expression))

    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.

    Does "a double-quote inside the expression is not treated specially" say
    that double-quotes are treated literally, i.e. excluded from quote
    removal, thus left as (a syntactic invalid) part of the arithmetic expression?

    My interpretation would be that it's effectively just ignored because
    the expression is anyway treated like being double-quoted. It would,
    IMO, also make no sense to assume differently. Throw an error for a
    quoted $-expression?!

    But shells behave differently, so maybe it's not that clear...

    $ ksh -c 'v="6*7" ; printf "%s\n" "$((v))" ; printf "%s\n" $(("${v}"))'
    42
    42

    $ zsh -c 'v="6*7" ; printf "%s\n" "$((v))" ; printf "%s\n" $(("${v}"))'
    42
    zsh:1: bad math expression: illegal character: "

    $ bash -c 'v="6*7" ; printf "%s\n" "$((v))" ; printf "%s\n" $(("${v}"))'
    42
    bash: "6*7": syntax error: operand expected (error token is ""6*7"")

    At least bash and zsh seem to interpret it the way you suspected. (And
    as so often, I'm happy to use ksh here which serves my interpretation.)

    And dash complains completely...

    $ dash -c 'v="6*7" ; printf "%s\n" "$((v))" ; printf "%s\n" $(("${v}"))'
    dash: 1: Illegal number: 6*7


    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)