The Unix Heritage Society mailing list
 help / color / mirror / Atom feed
From: Brian Walden <tuhs@cuzuco.com>
To: <tuhs@minnie.tuhs.org>
Subject: Re: [TUHS] Arithmetic expansion in Unix shells
Date: Tue, 22 Jun 2021 11:52:36 -0400 (EDT)	[thread overview]
Message-ID: <202106221552.15MFqa97028493@cuzuco.com> (raw)

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

             reply	other threads:[~2021-06-22 16:18 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-22 15:52 Brian Walden [this message]
  -- strict thread matches above, loose matches on Subject: below --
2021-06-21  9:37 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=202106221552.15MFqa97028493@cuzuco.com \
    --to=tuhs@cuzuco.com \
    --cc=tuhs@minnie.tuhs.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).