How to Increment and Decrement Variable in Bash (Counter)

One of the most popular arithmetic operations when addressing Bash scripts is incrementing and decrementing variables. This is most regularly used in loops as a counter, but it can happen elsewhere in the script.

Incrementing and Decrementing centers on adding or subtracting a value (usually 1), respectively, from the value of a numeric variable. The arithmetic augmentation can be performed using the double parentheses ((…)) and $((…)) or with the built-in let command.

In bash, there are various ways to increment/decrement a variable. In this article, some are explained.

Using + and – Operators

The most easy way to increment/decrement a variable is by using the + and – operators.

i=$((i+1))

((i=i+1))

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

((i=i-1))

let "i=i-1"

This method enables you to increment/decrement the variable by any value you want.

Here is an example of incrementing a variable within an until loop:

i=0

until [ $i -gt 3 ]

do

  echo i: $i

  ((i=i+1))

done

Output:

i: 0

i: 1

i: 2

i: 3

The += and -= Operators

In addition to the basic operators explained above, bash also provides the assignment operators += and -=. These operators are used to increment/decrement the left operand’s value with the value specified after the operator.

((i+=1))

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

let "i-=1"

Copy

In the example below, we will show how to decrement the I variable’s value by 5.

i=20

while [ $i -ge 5 ]

do

  echo Number: $i

  let "i-=5" 

done

Output:

Number: 20

Number: 15

Number: 10

Number: 5

Using the ++ and — Operators

The ++ and — operators increment and decrement, respectively, its operand by 1 and return the value.

((i++))

((++i))

let "i++"

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

((--i))

let "i--"

let "--i"

The operators can be used before or after the operand. They are also known as:

  • prefix increment: ++i
  • prefix decrement: –i
  • postfix increment: i++
  • postfix decrement: i–

The prefix operators first increment/decrement the operators by one and then return the new value to the operator. On the other hand, the postfix operators return the operators` value before incrementing/decremented the operator’s value.

If you only want to increment/decrement the variable, then there is no difference if you use the prefix or postfix operator. It only makes a difference if the operators’ result is accepted in some other operation or assigned to another variable.

The example below will show how the ++ operator works when is used before and after its operant:

x=5

y=$((x++))

echo x: $x

echo y: $y

Output

x: 6

y: 5
x=5

y=$((++x))

echo x: $x

echo y: $y
Copy

x: 6

y: 6

The example below shows us how to use the postfix incrementor in a bash script:

#!/bin/bash

i=0

while true; do

  if [[ "$i" -gt 3 ]]; then

    exit 1

  fi

  echo i: $i

  ((i++))

done

The downside of using these operators is that the variable can only be incremented or decremented by one only.

Conclusion                      

In bash, incrementing and decrementing variables can be performed in many different ways. So whatever method you use, the result is the same. If you have any query related to this, please write us.

Leave a Reply

Your email address will not be published. Required fields are marked *