bash increment and decrement-variables

When writing Bash scripts, one of the most common arithmetic operations is Incrementing and Decrementing variables.

Generally, it is used in a loop as a counter. Sometimes you will need to write while loop where you need to define increment or decrement variables for the normal function of loops.

There are several ways to increment and decrement a variable in Bash.

In this tutorial, we will show you how to increment and decrement a variable in Bash.

Using + and – Operators

You can use increment operator (+) increase the value of the variable by one and decrement operator (-) decrease the value of the variable by one in Bash.

The basic syntax of increment operator (+) and decrement operator (-) are shown below:

Increment Operator

i=$((i+1))
((i=i+1))
let "i=i+1"

Decrement Operator

i=$((i-1))
((i=i-1))
let "i=i-1"

Let’s see an example of incrementing a variable by one in until loop.

nano test.sh

Add the following code:

#!/bin/bash
count=0
until [ $count -gt 4 ]
do
echo count: $count
((count=count+1))
done

Save and close the file when you are finished. Then, run the script as shown below:

bash test.sh

You should see the following output:

count: 0
count: 1
count: 2
count: 3
count: 4

In the above program, the value of count is incremented one by one from 1 up to 4 using + operator.

You should see all the commands in the following screen:

Next, see an example of decrementing a variable by one in until loop.

nano test.sh

Add the following code:

#!/bin/bash
count=5
until [ $count -le 0 ]
do
echo count: $count
((count=count-1))
done

Save and close the file when you are finished. Then, run the script as shown below:

bash test.sh

You should see the following output:

count: 5
count: 4
count: 3
count: 2
count: 1

In the above program, the value of count is decremented one by one from 5 up to 0 using operator.

You should see all the commands in the following screen:

Using += and -= Operators

Bash also provides the assignment operators += and -= to increment and decrement the value of the left operand with the value specified after the operator.

The basic syntax of increment (+=) and decrement (-=) operators are shown below:

Increment (+=) Operators

((i+=1))
let "i+=1"

Decrement (-=) Operators

((i-=1))
let "i-=1"

Let’s see an example of incrementing the value of the variable count by 3.

nano test.sh

Add the following code:

#!/bin/bash
count=0
while [ $count -le 12 ]
do
echo Number: $count
let "count+=3"
done

Save and close the file when you are finished. Then, run the script with the following command:

bash test.sh

You should see the following output:

Number: 0
Number: 3
Number: 6
Number: 9
Number: 12

You should see the output of all the commands in the following screen:

Let’s see another example of decrementing the value of the variable count by 4.

nano test.sh

Add the following code:

#!/bin/bash
count=40
while [ $count -ge 4 ]
do
echo Number: $count
let "count-=4"
done

Save and close the file when you are finished. Then, run the script with the following command:

bash test.sh

You should see the following output:

Number: 40
Number: 36
Number: 32
Number: 28
Number: 24
Number: 20
Number: 16
Number: 12
Number: 8
Number: 4

You should see the output of all the commands in the following screen:

Using the ++ and — Operators

Bash has two special unary operators increment (++) and decrement (–) operators. Increment (++) operator increases the value of a variable by 1 and decrement (–) operator decreases the value of a variable by 1, and return the value.

The basic syntax of the increment (++) and decrement (–) operators are shown below:

Increment (++) Operator

((i++))
((++i))
let "i++"
let "++i"

Decrement (–) Operator

((i--))
((--i))
let "i--"
let "--i"

Increment and Decrement operators are of two types:

  • Prefix increment and decrement operator : ++i and –i
  • Postfix increment and decrement operator :i++ and i–

In prefix operator (++i/–i), the value of i is incremented/decremented by 1 then, it returns the value.

In postfix operator (i++/i–), the original value of i is returned first then, i is incremented by 1.

Let’s see an example of how prefix (++i) increment operator works.

nano test.sh

Add the following code:

#!/bin/bash
a=10
b=$((a++))
echo a: $a
echo b: $b

Save and close the file when you are finished. Then, run the script as shown below:

bash test.sh

You should see the following output:

a: 11
b: 10

In the above example, the current value of a is assigned to b then a is incremented.

You should see all the commands in the following screen:

Next, let’s see an another example of how postfix (++i) increment operator works.

nano test.sh

Add the following code:

#!/bin/bash
a=10
b=$((++a))
echo a: $a
echo b: $b

Save and close the file when you are finished. Then, run the script as shown below:

bash test.sh

You should see the following output:

a: 11
b: 11

In the above example, the current value of a is incremented by 1. The new value of a is then assigned to b.

You should see all the commands in the following screen:

You can also use the postfix increment operator in a bash script.

To do so, let’s create a new bash script as shown below:

nano test.sh

Add the following code:

#!/bin/bash
a=0
while true; do
if [[ "$a" -gt 5 ]]; then
exit 1
fi
echo a: $a
((a++))
done

Save and close the file when you are finished. Then, run the script as shown below:

bash test.sh

You should see the following output:

a: 0
a: 1
a: 2
a: 3
a: 4
a: 5

You should see all the commands in the following screen:

If you want to use the postfix decrement operator in a bash script, let’s create a new bash script as shown below:

nano test.sh

Add the following code:

#!/bin/bash
a=5
while true; do
if [[ "$a" -le 0 ]]; then
exit 1
fi
echo a: $a
((a--))
done

Save and close the file when you are finished. Then, run the script as shown below:

bash test.sh

You should see the following output:

a: 5
a: 4
a: 3
a: 2
a: 1

You should see all the commands in the following screen:

Conclusion

In the above tutorial, we learned how to use increment and decrement operators in Bash script in several ways. We hope you’ve learned how to use these operators in Bash as per your requirements and needs.

Feel free to comment below if you have any questions.