Monday 14 August 2017

Bash Variable pre/post-increment and pre/post-decrement

Cited from the Book "Pro Bash Programming"

id++ id--     Variable post-increment and post-decrement

++id –-id     Variable pre-increment and pre-decrement

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In languages that support both versions of the operators, the pre-increment and pre-decrement operators increment (or decrement) their operand by 1, and the value of the expression is the resulting incremented (or decremented) value. In contrast, the post-increment and post-decrement operators increase (or decrease) the value of their operand by 1, but the value of the expression is the operand's original value prior to the increment (or decrement) operation.

$ x=5; echo $(( ++x / 2 ))
3
$ x=5; echo $(( x++ / 2 ))
2

No comments:

Post a Comment