Email List: Xaustin-group-lX
[All Lists]

Re: shell, arithmetic expansion and integer constants

To: Roger Marquis <marquis@xxxxxxxxx>
Subject: Re: shell, arithmetic expansion and integer constants
From: Stephane Chazelas <Stephane_Chazelas@xxxxxxxx>
Date: Tue, 17 Jun 2008 16:26:36 +0100
Cc: austin-group-l@xxxxxxxxxxxxx
References: <20080617140716.GN10734@prunille.vinc17.org> <20080617144902.39B972B59A0@mx5.roble.com>
On Tue, Jun 17, 2008 at 07:49:02AM -0700, Roger Marquis wrote:
> Vincent Lefevre wrote:
>> $ echo $((x))
>> sh: 1+: syntax error: operand expected (error token is "+")
>
> That's bash syntax

No that's POSIX syntax as specified by the opengroup.

> but the error indicates Bourne sh

No, it indicates "sh". The Bourne shell was the implementation
of the "sh" utility started from Unix V7 and until it was
replaced by an open and now free specification which bash, ksh
and many other shells implement.

> Bourne is a
> standard across Unix/Linux OS

No, the Bourne shell has never been "sh" on Linux for instance.
The Bourne shell is slowly fading away. Many Unix distributions
don't ship it anymore.

An there have been various variants and versions of the Bourne
shell.

> whereas bash is, well, bash comes several versions.

But bash, when called as "sh" is meant to conform to that sh
language spec. So that if you write your script in that
language, you don't need to worry what shell interprets it as
long as it's a conforming shell.

I agree with Vincent that there the area of arithmetic expansion
could be clarified. Like is it OK for a conforming application
(read: script) to use:

y=$((x = x * 1))

And is there a restriction on the value of $x. Does "integer
constant" include negative values or values like +3...

Does $((x)) being the same as $(($x)) mean that $((x + 1)) is
also the same as $(($x + 1)) or that x=-1; $((1 $x)) is the same
as x=-1; $((1 x))?

> PS. Where is the online POSIX Bourne shell spec?

That is contradictory. The Bourne shell is not POSIX sh
conformant. The Bourne shell is something from the past, like
the Thomson shell (also called sh) was made something from the
past when the Bourne shell came up.

The POSIX sh (and utilities) spec is now embedded in the Single
Unix Specification which you can find on the opengroup web site.

> PPS. Common syntax must retain backwards compatibility.  It was difficult
>  enough having to add LANG=C and _POSIX2_VERSION=199209 to dozens of 
> scripts
>  after the syntax for sort was changed (without adequate involvement of the
>  sysadmin community IMO) just to be able to sort by syslog datestamps. Date
>  sorting is one of the most common sort usages, but was left out of the
>  POSIX spec, and dropped wholesale by GNU implementations.

Note that the POSIX sh is not fully backward compatible with the
Bourne shell which explains why some vendors like Solaris still
keep their Bourne shell as /bin/sh and have the standard sh in
another place (which POSIX/SUS doesn't prohibit, but still
causes pain to anyone wanting to write portable scripts).

-- 
Stéphane

<Prev in Thread] Current Thread [Next in Thread>