Bash Increment Counter in Ubuntu

  • Post author:
  • Post comments:0 Comments
  • Reading time:4 mins read
Bash Increment Counter in Ubuntu

Every Linux user or server admin loves bash scripting as it makes their work very easy. When writing bash scripts we have surely come across the term arithmetic operators especially increment operators which are used for bash increment counter.

Incrementing usually refers to adding value to a numeric variable. It is mostly used as a counter in a loop. There is more than one way to increment a variable in bash.

We can obtain this using $((….)), ((….)) or simply with build-in command let.

In this tutorial with practical examples, we will learn some of the ways to increment the counter in bash.

Incrementing Counter Using (+) Increment Operator in Bash

The simplest and most used way to increment a counter is by using the (+) increment operator. We can use the (+) operator to increment the variable value by any value we want.

The basic syntax of the (+) operator is given below.

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

Now let’s see one example of incrementing variable using (+) increment operator in a while loop.

In the below example, we set the initial value of the counter to 1 and with each loop, we will be increasing the counter value by 1. Our while loop will be running till the counter value is less than or equal to 10.

!/bin/bash

counter=1
while [ $counter -le 10 ]
do
     echo Counter Value is : $counter
     ((counter=counter+1))
done
Incrementing Counter Using (+) Increment Operator in Bash

When we execute the above code we will get output as seen below.

Incrementing Counter Using (+) Increment Operator in Bash

Now let’s see another example of incrementing variable using (+) increment operator in an until loop.

In the below example, we set the initial value of the counter to 1 and with each loop, we will be increasing the counter value by 1. Our until loop will be running till the counter value becomes greater than or equal to 10.

!/bin/bash

counter=1
until[ $counter -ge 10 ]
do
     echo Counter Value Reached : $counter
     ((counter=counter+1))
done
Incrementing Counter Using (+) Increment Operator in Bash

When we execute the above code we will get output as seen below.

Incrementing Counter Using (+) Increment Operator in Bash

Incrementing Counter Using (+=) Assignment Operator in Bash

Another famous operator used for incrementing the variable in bash is the (+=) assignment operator. In this value of the left operand is incremented with the value specified after the operator.

The basic syntax of (+=) assignment operator is given below.

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

Below is the example of incrementing variable using the (+=) assignment operator in a while loop.

In below example we will be incrementing the counter value by one using assignment operator.

!/bin/bash

counter=0
while [ $counter -le 10 ]
do
    echo Counter Value is : $counter
    ((counter+=1))
done
Incrementing Counter Using (+=) Assignment Operator in Bash

Output of above code is given below.

Incrementing Counter Using (+=) Assignment Operator in Bash

Incrementing Counter Using Increment Operator (++) Unary Operator in Bash

Bash also provides one special unary increment operator (++). This operator increment it’s operand value by 1 and returns the value.

Unary increment operator (++) are of two types:

  1. Prefix Increment Operator : ++i
  2. Postfix Increment Operator : i++

The prefix operator (++i) first increments the value of i then it returns the value.

The postfix operator (i++) first returns the original value of i then it increments the value of i by 1.

Basic syntax of both prefix and postfix unary increment operators are given below.

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

Let’s see one example of prefix increment counter (++i)

In the below example, we will be incrementing the value of the counter by 1 using prefix increment operator (++i).

!/bin/bash

counter=0
while [ $counter -le 5 ]
do
echo Counter Value is : $counter
((++counter))
done
Incrementing Counter Using Increment Operator (++) Unary Operator in Bash

Output of above code is given below.

Incrementing Counter Using Increment Operator (++) Unary Operator in Bash

Let’s see another example of postfix increment counter (i++)

In the below example, we will be incrementing the value of the counter by 1 using postfix increment operator (i++).

!/bin/bash

counter=0
while [ $counter -le 5 ]
do
echo Counter Value is : $counter
((counter++))
done
Incrementing Counter Using Increment Operator (++) Unary Operator in Bash

Output of above code is given below.

Incrementing Counter Using Increment Operator (++) Unary Operator in Bash

Conclusion

In this tutorial, we learned how to increment variable or counter using various operators in the bash shell in ubuntu.

Leave a Reply