Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #14321

Re: Number with sign is read as octal despite a leading 10#

From Ilkka Virta <itvirta@iki.fi>
Newsgroups gnu.bash.bug
Subject Re: Number with sign is read as octal despite a leading 10#
Date 2018-07-10 21:41 +0300
Message-ID <mailman.3391.1531248126.1292.bug-bash@gnu.org> (permalink)
References <CA+n9pTy7vSPUA0j-bDF9_eWF0XysePRDAFckgvvHX8haSWv7KQ@mail.gmail.com> <c8ae5df2-b6b3-438f-bd99-4618f6b2d3c0@Spark> <71850c03-54d3-6a7e-1d29-136950d9e139@iki.fi> <a0b100e7-3e14-e56e-8ffb-fcaeca587bf1@case.edu>

Show all headers | View raw


On 10.7. 18:09, Chet Ramey wrote:
> On 7/10/18 6:44 AM, Ilkka Virta wrote:
>> I think the problematic case here is when the number comes as input from
>> some program, which might or might not print a leading sign or leading
>> zeroes, but when we know that the number is, in any case, decimal.
>>
>> E.g. 'date' prints leading zeroes, which is easy enough to handle:
>>
>> hour=$(date +%H)
>>
>> hour=${hour#0}         # remove one leading zero, or
>> hour="10#$hour"        # make it base-10
>>
>> The latter works even with more than one leading zero, but neither works
>> with a sign. So, handling numbers like '-00159' gets a bit annoying:
> 
> That is not an integer constant. Integer constants don't begin with `-'.
> Bash uses the same definition for constants as the C standard, with the
> addition of the `base#value' syntax.

At least from my point of view this isn't necessarily a bug, more like a 
feature request. The behaviour matches the description you just wrote, 
and also the documentation. That doesn't mean it's the only possible 
behaviour.

Changing the parsing here could be useful, and would improve 
compatibility with ksh and zsh. I don't think there's any alternative 
sensible meaning for  10#-0123  anyway, but I might be mistaken.

> Since the `10#' notation is sufficient to deal with leading zeroes if you
> want to force decimal, you only have to remove a leading unary plus or
> minus.

Which I thought I just did, and Pierre provided a better way to do it 
(thanks).

Not having to splice the sign around would make this somewhat easier 
though, but YMMV.

-- 
Ilkka Virta / itvirta@iki.fi

Back to gnu.bash.bug | Previous | Next | Find similar | Unroll thread


Thread

Re: Number with sign is read as octal despite a leading 10# Ilkka Virta <itvirta@iki.fi> - 2018-07-10 21:41 +0300

csiph-web