On 11/30/23 5:18 AM, Martin D Kealey wrote:

When I said:

     > This change will break scripts that use $((10#$somevar)) to cope with
     > somevar having leading zeroes OR BEING EMPTY.


Chet replied:

    this clearly invalid syntax


"Clearly Invalid" is a matter of perspective. To me "no digits at all" is the MOST logical way to write value zero, being the logical result of "remove leading zero digits".

OK, this is worth discussion. So you write your hex constant 0 as "0x",
like the subject of this thread suggests? Where do you think that's going
to be valid? Or something like

"" + 7

should be 7? Now, bash does accept this, but no one else does (a
consequence of the bash arithmetic evaluator trying to be extra-friendly).
None of it is valid C arithmetic syntax, which is what we're all supposed
to be using for this. Hell, according to the C standard, anything with a
leading zero is an octal constant by definition (unless after a
hexadecimal-prefix). There is no decimal constant `0'.

In the end, this strategy of removing leading zeros breaks down as
identical to the accumulator strategy of arithmetic constant evaluation:
tot = (tot * base) + digit_value. There's no separate "remove leading
zeroes" step. But there's no valid C constant that has zero digits
anyway, so this is all academic.

(I may be sufficiently atypical that nobody on this list agrees with me, but I'm definitely not unique globally.)

Sure, it's a big world.  But is it a popular enough view that it would do
more than confuse people?


That change introduced an inconsistency into Bash:

  X= ; ((X == 0))  # true
  X= ; ((10#$X == 0)) # was true, now broken

These are not the same. If you want to make the examples the same in terms
of what the arithmetic evaluator sees, write

        (( $X == 0 ))

since word expansion happens before the `((' compound command runs.


Chet said:

    You might be interested in the discussion:


    https://lists.gnu.org/archive/html/bug-bash/2018-07/msg00015.html
    <https://lists.gnu.org/archive/html/bug-bash/2018-07/msg00015.html>
    https://lists.gnu.org/archive/html/bug-bash/2019-06/msg00039.html
    <https://lists.gnu.org/archive/html/bug-bash/2019-06/msg00039.html>


I read that and wept.

Just because someone didn't understand the difference between a single token and an entire arithmetic expression doesn't mean "change the definition of a token" was the right response.

In both examples, the OP didn't understand that `-' is an operator. It's
not part of an integer constant. You're talking about the definition of an
integer constant, not that the `definition of a token' got changed (unless,
of course, you're talking about the specific integer constant token type).


Even if this seemed to be clearly the reasonable response, why was there no clear & separate solicitation of feedback for "we propose to invalidate "10#" without any following digits"?

It came up as a bug report in

https://lists.gnu.org/archive/html/bug-bash/2019-06/msg00042.html

(part of the followup discussion after the second linked thread above)
and the consensus among those who participated was that it was a good
thing to prevent base# without any digits from silently being treated
as 0. Do you think there would have been more discussion in different
circumstances? Would you have participated, considering there's no sign
of you on the bug-bash list between 2016 and 2020?

     >> However, a somewhat similar situation with hex prefix,
     >> 0xDIGITS, still allows just "0x" as a valid zero constant.
     >>
     >> Not sure whether this should be considered a bug,
     >> and whether it's worth fixing - just letting you know.
     >>
     >
     > No, definitely not. It's established behaviour, and should not be
    removed
     > without an explicit shopt.

    It's clearly a bug -- it's an invalid C constant. I suppose we can call
    it an extension.


"Clearly a bug" is a matter of opinion,

Come on. Bash (and POSIX) define arithmetic in terms of how C does it,
and that is an invalid C integer constant. It's not even shell-specific
syntax like base#number; it's something that C defines. Is it worth it
trying to be helpful, or is it better to follow the standard you say you
do?

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