Increment and Decrement

Learn how increment and decrement operations work in Bash.

The increment and decrement operations first appeared in the experimental programming language developed it in 1969 by B. Ken Thompson and Dennis Ritchie while working at Bell Labs. Dennis Ritchie later moved these operations to his new language called C. Bash later copied these behaviors from C.

Assignment operations

First, let’s consider assignment operations to help us understand how increment and decrement work. A regular assignment in arithmetic evaluation looks like this:

((var = 5))

This command assigns the number 5 to the var variable.

Bash allows you to combine an assignment with arithmetic or bitwise operation. The following command does addition and assignment at the same time:

((var += 5))

The command performs two actions:

  1. It adds the number 5 to the current value of the var variable.

  2. It writes the result back to the var variable.

All other assignment operations work the same way. First, they do a mathematical or bitwise operation. Second, they assign the result to the variable. Using assignments makes our code shorter and clearer to read.

Increment and decrement operations

Now, let’s consider the increment and decrement operations. They have two forms: postfix and prefix. They are used in different ways. In the postfix form, the ++ and-- signs come after the variable name. In the prefix form, they come before the variable name. Here is an example of the prefix increment:

Here is an example of the prefix increment:

((++var))

This command provides the same result as the following assignment operation:

((var+=1))

The increment operation increases the variable’s value by one. Decrement decreases it by one.

Why does it make sense to introduce special operations for adding and subtracting one? After all, the Bash language has assignments += and -= that we can use instead.

The most likely reason for introducing the increment and decrement is to easily manage a loop counter. This counter stores the number of loop iterations. When we want to interrupt the loop, we check its counter in a condition statement. The result of the condition statement defines if we should interrupt the loop or not.

Increment and decrement make it easier to serve the loop counter. Besides that, modern processors perform these operations on the hardware level. Therefore, they work faster than addition and subtraction combined with the assignment.

What is the difference between prefix and postfix forms of increment? If the expression consists only of an increment operation, we get the same result for both forms.

For example, the following two commands increase the variable’s value by one:

((++var))
((var++))

The difference between the increment forms appears when we assign the result to a variable. Here is an example:

var=1
((result = ++var))

Run the commands discussed in this lesson in the terminal below.

Get hands-on with 1200+ tech skills courses.