Reputation: 1197
ARITHMETIC EVALUATION section in bash manual lists following operators among others:
id++ id--
variable post-increment and post-decrement
++id --id
variable pre-increment and pre-decrement
As I understand, ++x
and --xx
increase or decrease the variable before other operations are performed? For example x++
:
$ x=5; echo $(( ++x / 2 ))
3
$ x=5; echo $(( x++ / 2 ))
2
$
However, when are x++
and x--
useful? And in general what is the difference between variable post-increment/decrement and pre-increment/decrement in bash?
Upvotes: 0
Views: 2038
Reputation: 155
pre-inrement/pre-decrement means to change the value then do what you intend with the changed value: change it firs then use it. Sometimes I write a counter variable on the fly for a 'for' loop and each iteration it increments by 1: counter=10 && counter2=0; for x in {1..10}; do printf "$((--counter)) $x myResult\n" && ((++counter2)); done && echo "there were $counter2 iterations"
gives this:
9 1 myResult 8 2 myResult 7 3 myResult 6 4 myResult 5 5 myResult 4 6 myResult 3 7 myResult 2 8 myResult 1 9 myResult 0 10 myResult there were 10 iterations
post-increment/decrement operators you wish to use (often existing values) and then change the variable: counter=10 && unset counter2; for x in {1..10}; do printf "$((counter--)) $x myResult\n" && ((counter2--)); done && echo "there were $counter2 iterations"
10 1 myResult 9 2 myResult 8 3 myResult 7 4 myResult 6 5 myResult 5 6 myResult 4 7 myResult 3 8 myResult 2 9 myResult 1 10 myResult there were 10 iterations
It all depends on what you need the script to show/do, and often when it should do that -- yet as in the example use of $counter2 , sometimes you would get the same result using either post- or pre- increment/decrement. The same can hold true in other coding: c/c++/c#/perl and probably go, rust, ruby etc. It's good to understand what is happening with it and when you need a particular result.
Upvotes: 0
Reputation: 123648
Quoting from Increment and decrement operators:
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.
So, you'll find:
$ x=5; echo $(( x++ / 2 ))
2
$ echo ${x} // The effect of post-increment is visible here
3
Upvotes: 1
Reputation: 5295
Both post-ops and pre-ops change (increase/decrease) value of the variable.
The difference is what they evaluate to: pre-ops evaluate to the value of the variable after the change, and post-ops - to the value before the change.
When the evaluated value isn't used, there is no difference. I.e. these two lines have the same effect:
((x++))
((++x))
Both pre-ops and post-ops are used to remove the need for an explicit assignment. I.e. make code shorter. So, instead of writing this:
x=$((x + 1))
y=$((x * 5))
You can write this:
y=$((++x * 5))
Conversely, instead of this:
y=$((x * 5))
x=$((x + 1))
You can write this:
y=$((x++ * 5))
Most often these operations are used in loop bodies and loop control expressions.
Upvotes: 1