Re: bug in arithmetic expansion
Date:Sat, 9 Nov 2019 16:39:52 +0100 From:Davide Brini Message-ID: <1mi5ud-1ifip305pl-00f...@mail.gmx.com> | If you want to force base 10 interpretation (remember that leading 0 mean | octal in arithmetic context), you need to explicitly tell bash: | | $ echo $(( 10#0123 )) | 123 But do remember that that form is not portable, and is difficult to use correctly in the cases that matter (when the actyal number comes from a variable .. when it is literal, as in all the examples in this thread, simply omitting the leading 0 is much simpler, and fully portable). kre
Re: bug in arithmetic expansion
On Sat, 9 Nov 2019 11:52:56 +0100, Joern Knoll wrote: > [tplx99]:/the/knoll > echo $((0123)) > 83 > [tplx99]:/the/knoll > echo $((123)) > 123 > [tplx99]:/the/knoll > echo $((01234)) > 668 > [tplx99]:/the/knoll > echo $((1234)) > 1234 If you want to force base 10 interpretation (remember that leading 0 mean octal in arithmetic context), you need to explicitly tell bash: $ echo $(( 10#0123 )) 123 -- D.
Re: bug in arithmetic expansion
In the arithmetic context, leading zeroes signify an octal base. Had you used an 8 or 9, you would have gotten a message like: bash: 08: value too great for base (error token is "08") when trying: echo $((08)) So it's not a bug, it's a feature; make sure your base-10 numbers don't have leading zeroes! Peter On 11/9/19 5:52 PM, Joern Knoll wrote: > Hallo, > > in playing around with digital keys (integers) which have a simple > arithmetic check property, I encountered problemsusing bash's arithmetic > expansion, when ever the used digital substrings have leading zeros. The > problem shows up already for the simplest operations, namely converting > a string argument to its numerical value, as shown below. > > With thanks for your attention and best regards, Jörn Knoll > > [tplx99]:/the/knoll > echo $((0123)) > 83 > [tplx99]:/the/knoll > echo $((123)) > 123 > [tplx99]:/the/knoll > echo $((01234)) > 668 > [tplx99]:/the/knoll > echo $((1234)) > 1234 > >
bug in arithmetic expansion
Hallo, in playing around with digital keys (integers) which have a simple arithmetic check property, I encountered problemsusing bash's arithmetic expansion, when ever the used digital substrings have leading zeros. The problem shows up already for the simplest operations, namely converting a string argument to its numerical value, as shown below. With thanks for your attention and best regards, Jörn Knoll [tplx99]:/the/knoll > echo $((0123)) 83 [tplx99]:/the/knoll > echo $((123)) 123 [tplx99]:/the/knoll > echo $((01234)) 668 [tplx99]:/the/knoll > echo $((1234)) 1234 -- % % % Jörn Knoll phone: +49 6159 71 2753 % % GSI fax: +49 6159 71 2990 % % Planckstr. 1email:j.kn...@gsi.de % % D-64291 Darmstadthttps://theory.gsi.de % % % % GSI Student Programhttps://theory.gsi.de/stud-pro% % Schnelle Ionen e.V.https://www.SchnelleIonen.de %