Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: arithmetic operator precedence



On Tue, Jun 17, 2008 at 02:20:39PM +0100, Stephane Chazelas wrote:
> On Tue, Jun 17, 2008 at 03:02:46PM +0200, Vincent Lefevre wrote:
> [...]
> > It is unspecified. So, the shell is right to choose how it sees it.
> > 
> > > If $a contains an integer constant such as -3, then as per
> > > POSIX, $((a * 3)) should be the same as $(($a * 3)), that is
> > > $((-3 * 3)).
> > 
> > No, POSIX does not say that. It happens to be the same thing here
> > just because of the properties of *, but you can't deduce anything
> > for extensions.
> 
> It says $((x)) is meant to be the same as $(($x)) which I
> understand as any occurrance of a variable name (other than $-,
> $?, $0... obviously) in $((...)) should be the same as if the $
> was not ommited (when $x contains an integer constant).
[...]

It can't be that simple though, as in

x=-1; $((1 x))

which would be POSIX but fails in all shells.

Or

x=1; $((x = 1 + x))

which obviously is not meant to be the same as $(($x = 1 + $x))

So that

  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.

says either too little or too much to be useful.

-- 
Stéphane



Messages sorted by: Reverse Date, Date, Thread, Author