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

Re: shell, arithmetic expansion and integer constants

To: austin-group-l@xxxxxxxxxxxxx
Subject: Re: shell, arithmetic expansion and integer constants
From: Geoff Clare <gwc@xxxxxxxxxxxxx>
Date: Tue, 17 Jun 2008 16:24:24 +0100
References: <20080617140716.GN10734@prunille.vinc17.org>
Vincent Lefevre <vincent-opgr@vinc17.org> wrote, on 17 Jun 2008:
>
> Concerning arithmetic expansion, POSIX says[*]:
> 
>   If the expression is invalid, the expansion fails and the shell
>   shall write a message to standard error indicating the failure.
> 
> [*] 
>http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_04
> 
> But it doesn't seem to say anything if a variable contains an
> invalid value.

I think this was intentional, in order to allow implementations
to support things like x=1+1 if they wish.

> It doesn't either say what values are regarded as
> valid (this is OK, but see below). Some values are required to
> be recognized ("Only the decimal-constant, octal-constant, and
> hexadecimal-constant constants specified in the ISO C standard,
> Section 6.4.4.1 are required to be recognized as constants."),
> but note that this doesn't even include negative integers.

Ouch.  Good point about negative values.

> What I'm thinking of is something like the following:
> 
> $ x="1+"
> $ echo $((x))
> sh: 1+: syntax error: operand expected (error token is "+")
> 
> i.e. the expression $((x)) is valid, but not the value of x.
> 
> Also, POSIX says:
> 
>   If the shell variable x contains a value that forms a valid integer
>   constant, then the arithmetic expansions "$((x))" and "$(($x))"
>   shall return the same value.
> 
> but it also explicitly says that a real-floating type can be used
> instead of signed long, and I think that the equivalence between
> "$((x))" and "$(($x))" should be required even through extensions.

I don't think POSIX can place requirements on the behaviour of
extensions (other than the obvious one that they are compatible,
i.e. don't conflict, with the standard).  If we wanted to require
equivalence of $((x)) and $(($x)) for real-floating values, we would
have to specify real-floating support as an option, rather than just
an allowed extension.

> So, to summarize:
> 
> Shouldn't "valid integer constant" be replaced by "valid constant",
> and say that if the value is invalid then the shell shall write a
> message to standard error indicating the failure?

No and maybe.  (The maybe for the second part is because it would need
to be worded very carefully to ensure we don't disallow x=1+1.)

> Shouldn't POSIX require that negative integers are recognized (I'd say
> that the format should follow more or less what is described under the
> printf utility) and say that other valid forms are not specified?

Yes and maybe.  (The maybe for the second part is because I'm not
sure an explicit statement is needed - currently it is unspecified
by virtue of there being no statement about it.)

-- 
Geoff Clare <g.clare@opengroup.org>
The Open Group, Thames Tower, Station Road, Reading, RG1 1LX, England

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