bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Question about arithmetic expression grammar


From: Conrad Hoffmann
Subject: Question about arithmetic expression grammar
Date: Sat, 8 Oct 2016 17:33:00 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.3.0

Hi,

I have recently been looking at the POSIX Shell Command Language
specification [1] and trying to understand how shells parse their input.
One thing I am currently looking at is arithmetic expansion. I did some
tests with Bash, and one thing seemed curious to me:

In the Bash Reference Manual [2], the operator precedence is listed as:

1. post-in/decrement
2. pre-in/decrement
3. unary +/-
(etc...)

I was wondering about the implications of the ordering of the
pre/post-in/decrement operators for parsing. I came up with the
following expression to test this:

  $ TEST=5; echo $((--TEST+++3)) # outputs 7

However, due to the documented operator precedence, I would have
expected that expression to be equal to:

  $ TEST=5; echo $((--(TEST++)+3)) # outputs 8

Instead, though, it seems to be equal this one:

  $ TEST=5; echo $(((--TEST)+++3)) # outputs 7

So my qestions are:

Is this a bug? Or is this something that can't be resolved due
ambiguities in the grammar? Or what's going on here at all?

and:

Is there an expression where the precedence of the postfix operator
actually comes into play?

I am aware that this is a very fabricated example, I am just trying to
understand the boundaries of the grammar here. Any enlightenment would
be highly appreciated :)

Thanks a lot,
Conrad

[1] http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.htm
[2] https://www.gnu.org/software/bash/manual/bashref.html#Shell-Arithmetic



reply via email to

[Prev in Thread] Current Thread [Next in Thread]