The Unix Heritage Society mailing list
 help / color / mirror / Atom feed
* [TUHS] Arithmetic expansion in Unix shells
@ 2021-06-21  9:37 Michael Siegel
  2021-06-21  9:57 ` arnold
  0 siblings, 1 reply; 7+ messages in thread
From: Michael Siegel @ 2021-06-21  9:37 UTC (permalink / raw)
  To: tuhs

Hello,

I'm currently trying out the rc shell (using Byron Rakitzis'
implementation for Unix). Compared to Bash, which I normally use, this
shell has a rather small feature set (which isn't to say that that's
necessarily a bad thing).

Now, one of the features that Bash has and rc doesn't have is the
ability to perform arithmetic expansion. That's not really a problem
because you can just use `expr` instead. I wonder, though, when
arithmetic expansion as a shell built-in became a thing, especially in
POSIX sh.

POSIX has included that feature since at least 2001, and probably quite
some years earlier, given that Bash already had it in 1995 (going by
the manual page of version 1.14.7, the oldest I could find).

So, maybe someone here can help me find out when this was actually
standardized.

Thanks.

--
Michael

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [TUHS] Arithmetic expansion in Unix shells
  2021-06-21  9:37 [TUHS] Arithmetic expansion in Unix shells Michael Siegel
@ 2021-06-21  9:57 ` arnold
  2021-06-21 13:50   ` Chet Ramey
  0 siblings, 1 reply; 7+ messages in thread
From: arnold @ 2021-06-21  9:57 UTC (permalink / raw)
  To: tuhs, msi

Arithmetic expansion dates back at least as far as ksh88.
Bash likely picked it up from there.

The original was only integer math and Bash remains that way (IIRC,
Chet can correct me if I'm wrong). ksh93 added floating point math.

POSIX would have picked it up from ksh88.

HTH,

Arnold

Michael Siegel <msi@malbolge.net> wrote:

> Hello,
>
> I'm currently trying out the rc shell (using Byron Rakitzis'
> implementation for Unix). Compared to Bash, which I normally use, this
> shell has a rather small feature set (which isn't to say that that's
> necessarily a bad thing).
>
> Now, one of the features that Bash has and rc doesn't have is the
> ability to perform arithmetic expansion. That's not really a problem
> because you can just use `expr` instead. I wonder, though, when
> arithmetic expansion as a shell built-in became a thing, especially in
> POSIX sh.
>
> POSIX has included that feature since at least 2001, and probably quite
> some years earlier, given that Bash already had it in 1995 (going by
> the manual page of version 1.14.7, the oldest I could find).
>
> So, maybe someone here can help me find out when this was actually
> standardized.
>
> Thanks.
>
> --
> Michael

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [TUHS] Arithmetic expansion in Unix shells
  2021-06-21  9:57 ` arnold
@ 2021-06-21 13:50   ` Chet Ramey
  2021-06-21 14:43     ` Michael Siegel
  0 siblings, 1 reply; 7+ messages in thread
From: Chet Ramey @ 2021-06-21 13:50 UTC (permalink / raw)
  To: arnold, tuhs, msi

On 6/21/21 5:57 AM, arnold@skeeve.com wrote:
> Arithmetic expansion dates back at least as far as ksh88.

ksh had the `let' builtin from at least 1983. The ((...)) compound command
was there by ksh-86.

> Bash likely picked it up from there.

Sort of, see below.

> The original was only integer math and Bash remains that way (IIRC,
> Chet can correct me if I'm wrong). ksh93 added floating point math.

Yes, bash only has integer arithmetic, since it's all POSIX requires.

> POSIX would have picked it up from ksh88.

The $((...)) form of arithmetic expansion is something POSIX picked up
from ksh-88, eventually. The early drafts of the standard (through 1003.2
d9, at least), used $[...], but they eventually adopted $((...)) because
ksh-88 had already implemented it, though it's not documented in Bolsky
and Korn.

I put $[...] into bash first (it's still there, though deprecated), then
`let', then $((...)) after POSIX added it, and finally  `((' for
compatibility.

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
		 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [TUHS] Arithmetic expansion in Unix shells
  2021-06-21 13:50   ` Chet Ramey
@ 2021-06-21 14:43     ` Michael Siegel
  2021-06-21 14:48       ` Chet Ramey
  0 siblings, 1 reply; 7+ messages in thread
From: Michael Siegel @ 2021-06-21 14:43 UTC (permalink / raw)
  To: Chet Ramey; +Cc: tuhs

Am Mon, 21 Jun 2021 09:50:38 -0400
schrieb Chet Ramey <chet.ramey@case.edu>:

> The $((...)) form of arithmetic expansion is something POSIX picked up
> from ksh-88, eventually. The early drafts of the standard (through
> 1003.2 d9, at least), used $[...], but they eventually adopted
> $((...)) because ksh-88 had already implemented it, though it's not
> documented in Bolsky and Korn.

So, in other words, POSIX shell (meaning the shell described in the
standard as it has been released) had double-parentheses arithmetic
evaluation from the start?


--
Michael

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [TUHS] Arithmetic expansion in Unix shells
  2021-06-21 14:43     ` Michael Siegel
@ 2021-06-21 14:48       ` Chet Ramey
  2021-06-22 16:24         ` Michael Siegel
  0 siblings, 1 reply; 7+ messages in thread
From: Chet Ramey @ 2021-06-21 14:48 UTC (permalink / raw)
  To: Michael Siegel; +Cc: tuhs

On 6/21/21 10:43 AM, Michael Siegel wrote:
> Am Mon, 21 Jun 2021 09:50:38 -0400
> schrieb Chet Ramey <chet.ramey@case.edu>:
> 
>> The $((...)) form of arithmetic expansion is something POSIX picked up
>> from ksh-88, eventually. The early drafts of the standard (through
>> 1003.2 d9, at least), used $[...], but they eventually adopted
>> $((...)) because ksh-88 had already implemented it, though it's not
>> documented in Bolsky and Korn.
> 
> So, in other words, POSIX shell (meaning the shell described in the
> standard as it has been released) had double-parentheses arithmetic
> evaluation from the start?

 From the first published version of the standard, yes.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
		 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [TUHS] Arithmetic expansion in Unix shells
  2021-06-21 14:48       ` Chet Ramey
@ 2021-06-22 16:24         ` Michael Siegel
  0 siblings, 0 replies; 7+ messages in thread
From: Michael Siegel @ 2021-06-22 16:24 UTC (permalink / raw)
  To: Chet Ramey; +Cc: tuhs

Am Mon, 21 Jun 2021 10:48:18 -0400
schrieb Chet Ramey <chet.ramey@case.edu>:

> On 6/21/21 10:43 AM, Michael Siegel wrote:
> > So, in other words, POSIX shell (meaning the shell described in the
> > standard as it has been released) had double-parentheses arithmetic
> > evaluation from the start?  
> 
>  From the first published version of the standard, yes.

Thanks. This will spare me from putting yet another footnote into my
little review of rc(1).


--
Michael

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [TUHS] Arithmetic expansion in Unix shells
@ 2021-06-22 15:52 Brian Walden
  0 siblings, 0 replies; 7+ messages in thread
From: Brian Walden @ 2021-06-22 15:52 UTC (permalink / raw)
  To: tuhs

even tho it was there, in-shell integer arithmetic had a few
issues before ksh86.

pre-ksh86 these had problems --

A. decimal point in a number
$ integer I
$ I=2.2

would error on the assignment, but with ksh86 $I is truncated to 2

(yes "integer" is an builtin alias to typeset -i and ksh courses at the time
instructed to use that over typset)

B. negative numbers
$ integer I
$ I=-2

here pre-86 $I would be assigned a large value like 2147483646

also ksh was not stardard until SVR4 (very late 80s) so it was found
in paths like /usr/add-on/bin/ksh or /usr/exptools/bin/ksh, or not even
there at all, you could not reliably #! ksh scripts

also with ksh86 the double paren ((...)) notation was exactly the same as
"let ..." and were completely optional if the variable was predefined as
an integer, so

$ I=0
$ ((I=I+1))

 and

$ integer I
$ I=0
$ I=I+1

are the same.

All internal integers in ksh were C longs (at least 32-bits)

where Bourne shell all vars are strings so you would need to do this --

$ I=`expr $I + 1`

But also at that time expr(1) could NOT deal with negative numbers on input,
they became strings. So

$ expr -9 + 1

is an error with "non-numeric argument", and

$ expr -11 '<' -1

returns 0, a false statement, which are hidden bugs with variables.

expr(1) was also 32-bits, as was test (i.e [ ) which could deal with
negative numbers just fine.

for arbitrarily large numbers you needed use dc(1) or bc(1). but dc(1)
also has a issue with inputing negative numbers as it uses an _ not
a - to denote a negatve number on input, but does use the - on output.

$ I=`echo _9 1 - p | dc`

is how you would do ((I=-9 - 1)) in bourne with dc
which is cumbersome if you have a variable with a neagtive number in it,
and required a "tr - _" first.

however

$ I=`echo -9 - 1 | bc`

worked just fine in bourne.


-Brian

Chet Ramey wrote:
> On 6/21/21 5:57 AM, arnold at skeeve.com wrote:
> > Arithmetic expansion dates back at least as far as ksh88.
>
> ksh had the `let' builtin from at least 1983. The ((...)) compound command
> was there by ksh-86.
>
> > Bash likely picked it up from there.
>
> Sort of, see below.
>
> > The original was only integer math and Bash remains that way (IIRC,
> > Chet can correct me if I'm wrong). ksh93 added floating point math.
>
> Yes, bash only has integer arithmetic, since it's all POSIX requires.
>
> > POSIX would have picked it up from ksh88.
>
> The $((...)) form of arithmetic expansion is something POSIX picked up
> from ksh-88, eventually. The early drafts of the standard (through 1003.2
> d9, at least), used $[...], but they eventually adopted $((...)) because
> ksh-88 had already implemented it, though it's not documented in Bolsky
> and Korn.
>
> I put $[...] into bash first (it's still there, though deprecated), then
> `let', then $((...)) after POSIX added it, and finally  `((' for
> compatibility.
>
> Chet

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2021-06-22 16:25 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-21  9:37 [TUHS] Arithmetic expansion in Unix shells Michael Siegel
2021-06-21  9:57 ` arnold
2021-06-21 13:50   ` Chet Ramey
2021-06-21 14:43     ` Michael Siegel
2021-06-21 14:48       ` Chet Ramey
2021-06-22 16:24         ` Michael Siegel
2021-06-22 15:52 Brian Walden

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).