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

Re: arithmetic operator precedence



On 2008-06-17 15:35:59 +0100, Stephane Chazelas wrote:
> It can't be that simple though, as in
> 
> x=-1; $((1 x))
> 
> which would be POSIX but fails in all shells.

No, $((1 x)) is an invalid expression (unless the shell sees this
as an extension), so POSIX requires it to fail.

This is different from $((1 $x)), where the expansion of $x occurs
first.

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

So? What's the problem with them not being the same?

Note that POSIX says:

  The shell shall expand all tokens in the expression for parameter
                                                          ^^^^^^^^^
  expansion, command substitution, and quote removal.
  ^^^^^^^^^

  Next, the shell shall treat this as an arithmetic expression
  ^^^^

In the first case, the expression is x = 1 + x. In the second case,
it is 1 = 1 + 1.

> 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.

I agree that this isn't perfect (BTW, see my mail in the Austin group
when it appears -- I haven't received a copy yet).

-- 
Vincent Lefèvre <vincent@xxxxxxxxxx> - Web: <http://www.vinc17.org/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.org/blog/>
Work: CR INRIA - computer arithmetic / Arenaire project (LIP, ENS-Lyon)



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