Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > gnu.bash.bug > #14306
| Path | csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail |
|---|---|
| From | Ilkka Virta <itvirta@iki.fi> |
| Newsgroups | gnu.bash.bug |
| Subject | Re: Number with sign is read as octal despite a leading 10# |
| Date | Tue, 10 Jul 2018 14:14:39 +0300 |
| Lines | 93 |
| Approved | bug-bash@gnu.org |
| Message-ID | <mailman.3362.1531221288.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> |
| NNTP-Posting-Host | lists.gnu.org |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8; format=flowed |
| Content-Transfer-Encoding | quoted-printable |
| X-Trace | usenet.stanford.edu 1531221288 25842 208.118.235.17 (10 Jul 2018 11:14:48 GMT) |
| X-Complaints-To | action@cs.stanford.edu |
| To | Clint Hepner <clint.hepner@gmail.com>, bug-bash@gnu.org, bash@packages.debian.org, Isaac Marcos <isaacmarcos100010@gmail.com> |
| Envelope-to | bug-bash@gnu.org |
| User-Agent | Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:52.0) Gecko/20100101 Thunderbird/52.9.0 |
| In-Reply-To | <71850c03-54d3-6a7e-1d29-136950d9e139@iki.fi> |
| Content-Language | en-US |
| X-SASI-RCODE | 200 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; h=subject:from:to:references:message-id:date:mime-version:in-reply-to:content-type:content-transfer-encoding; s=smtp; bh=fKHKBai8ah8xAv2WCczlDvk1ksqfaYtRKKD3NddwGI0=; b=DS3lZNlhdcbDJT0cIngjkZupcCekOrdU+LZMzLDEs2U3AoODh4DyLjJcfUg5deWG1P/cbY+aI7QKmBKNpcvFNpQmabiZTTBvCO2FcPs7/0L4TWJVVUdy3X+WozEuXLs2ttMQWIXerGHUyEoXtAArDfi0mo/vb/LyiDt366440DrI4YkvuBGC30NyNvhjrkwUZZqKPFMXM2W1evw/kR4dEsZsW8OT1Gcd5iRuzDa1qq2b5VwnGpNUVlCJToCV7aPl/BFSH4I+s6yVx11g2kyL16QAvtTYuOG8r3UlLfdOJB917o4AF+lcNgqRzPKi3v1ZLUdeOGOwFbecw6Mcjz7wEw== |
| X-detected-operating-system | by eggs.gnu.org: FreeBSD 8.x [fuzzy] |
| X-Received-From | 157.24.2.104 |
| X-BeenThere | bug-bash@gnu.org |
| X-Mailman-Version | 2.1.21 |
| Precedence | list |
| List-Id | Bug reports for the GNU Bourne Again SHell <bug-bash.gnu.org> |
| List-Unsubscribe | <https://lists.gnu.org/mailman/options/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=unsubscribe> |
| List-Archive | <http://lists.gnu.org/archive/html/bug-bash/> |
| List-Post | <mailto:bug-bash@gnu.org> |
| List-Help | <mailto:bug-bash-request@gnu.org?subject=help> |
| List-Subscribe | <https://lists.gnu.org/mailman/listinfo/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=subscribe> |
| Xref | csiph.com gnu.bash.bug:14306 |
Show key headers only | View raw
For what it's worth, ksh and zsh seem to interpret 10#-0159
as negative one-hundred and fifty-nine:
$ ksh -c 'for a do a="10#$a"; printf "%s " $((a + 1)); done; echo' \
sh +159 +0159 -159 -0159
160 160 -158 -158
$ zsh -c 'for a do a="10#$a"; printf "%s " $((a + 1)); done; echo' \
sh +159 +0159 -159 -0159
160 160 -158 -158
$ ksh --version
version sh (AT&T Research) 93u+ 2012-08-01
$ zsh --version
zsh 5.3.1 (x86_64-debian-linux-gnu)
On 10.7. 13:44, 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:
>
> $ num='-00159'
> $ num="${num:0:1}10#${num:1}"; echo $(( num + 1 ))
> -158
>
> And that's without checking that the sign was there in the first place.
>
>
> Something like that will probably not be too common, but an easier way
> to force any number to be interpreted in base-10 (regardless of leading
> zeroes) could be useful. If there is a way, I'd be happy to hear.
>
>
> On 10.7. 04:37, Clint Hepner wrote:
>> The + is a unary operator, not part of the literal. Write
>> $((+10#0034)) instead.
>>
>> --
>> Clint
>> On Jul 9, 2018, 9:24 PM -0400, Isaac Marcos
>> <isaacmarcos100010@gmail.com>, wrote:
>>> Configuration Information [Automatically generated, do not change]:
>>> Machine: x86_64
>>> OS: linux-gnu
>>> Compiler: gcc
>>> Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64'
>>> -DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-pc-linux-gnu'
>>> -DCONF_VENDOR
>>> uname output: Linux IO 4.9.0-6-amd64 #1 SMP Debian 4.9.88-1+deb9u1
>>> (2018-05-07) x86_64 GNU/Linux
>>> Machine Type: x86_64-pc-linux-gnu
>>>
>>> Bash Version: 4.4
>>> Patch Level: 12
>>> Release Status: release
>>>
>>> Description:
>>> A value inside an arithmetic expansion is processed as octal despite
>>> using
>>> a 10# preffix.
>>>
>>> Repeat-By:
>>> $ echo $((10#+0034))
>>> 28
>>>
>>> Fix:
>>> Extract optional sign before parsing the number, re-attach after.
>>>
>>> --
>>> Cases are always threesome:
>>> Best case, Worst case, and Just in case
>
>
--
Ilkka Virta / itvirta@iki.fi
Back to gnu.bash.bug | Previous | Next | Find similar | Unroll thread
Re: Number with sign is read as octal despite a leading 10# Ilkka Virta <itvirta@iki.fi> - 2018-07-10 14:14 +0300
csiph-web