How to add or implement counter in bash scripting

The counter is very common in programming to trace the number of cycles that have been executed by loop functions.

In this article, you will see how to add or implement a counter in bash scripting.

Method to implement counter increment in bash scripting

There are two methods that are widely used to count a number 1.) let and 2.) Bash Arithmetic. And we will see the implementation in all the types of loops.

Use let command to count increment

let is a built-in command to evaluate the arithmetic operation. To implement in your script, first, see the simple way before using a loop.

The foremost step is to create a bash file. For that, you can use any command-line editor and paste the below code and make sure to save it.

We have created the “COUNTER” variable to store increment value which will be invoked by let or Bash Arithmetic Operations.

#!/bin/bash
# Create variable counter
COUNTER=0
echo "Initial value of COUNTER = "$COUNTER

# Basic Method
let COUNTER=COUNTER+1
echo "Output of COUNTER=COUNTER+ = "$COUNTER

# USE SHORTHAND METHOD
let COUNTER++
echo "Output of let COUNTER++ = "$COUNTER 

Once it is done, run the file.

$ bash counter.sh
use-let-command-for-counter
use let command for counter

If you just want to test the code and don’t want to create a file? For that, please copy the code from the above snippet and directly paste it to a terminal.

Bash Arthimetic Expression

Let’s see how to use the Bash arithmetic method to apply counters in your bash script. 

Before moving ahead, let’s clear your basics first, Although you know arithmetic evaluation formed in parenthesis preceding with symbol e.g., $(( 10 + 1 )).

#!/bin/bash
# Create variable counter and assign value
COUNTER=0
echo "Initial value of COUNTER is" $COUNTER

# Update counter value 
COUNTER=$(( COUNTER + 1 ))
echo "Output using arithmetic operation" $COUNTER

# Update counter value using shorthand 
(( COUNTER++ ))
echo "Output using shorthand assignment operators" $COUNTER

use arithmetic operation for counter
use arithmetic operation for counter

Implement counter using loop

We have multiple options available for loops such as for loop, while loop, until loop. I’ll try to cover all the options for you.

Implement counter using for loop

The below script will print numbers from 1 to 5 after that it will show you total counting. To monitor a number of iteration we have assigned a counter variable.

To test the script open your terminal, and create a script file using nano editor forloopmethod.sh and paste the below code.

#!/bin/bash
counter=0
for i in {1..5}
do
# Uncomment the below line for let to count                
# echo Number: "$i" && ((counter ++))
# Uncomment the below line for Bash Arithmetic to count 
# echo Number: "$i" &&  let counter++
done
echo "Total counting is: $counter"
$ bash forloopmethod.sh
For loop method to read counter
For loop method to read counter

Implement counter while loop

The below script will iterate numbers till 5 after that script will end with a total number of iteration. To see the result first create a script file and paste it and make sure to uncomment the respective method.

#!/bin/bash
counter=0
index=1
while [ $index -le 5 ]
do
# Uncomment the below line for let to count               
# echo Number: $index && let counter++
# Uncomment the below line for Bash Arithmetic to count   
# echo Number: $index && ((counter++))
   let index++
done
echo "Total number is: $counter"
$ bash forwhileloop.sh
For while loop to read counter
For while loop to read counter

Implement counter until loop

As I promised we will cover all the loop methods. This is the last method here we will print a number till 5, and once the pointer hits on 5 number it will show the total count.

While running a script make sure to remove comments else you may not get desired results.

$ bash untilloopmethod.sh
#!/bin/bash
counter=0
index=1
until [ $index -gt 5 ]
do
  echo Number: $index 
  ((index++)) && 
# Uncomment the below line for let to count 
# let counter++
# Uncomment the below line for Bash Arithmetic to count 
 ((counter++))
done
echo "Total number is: $counter"
until loop to read counter
Until loop to read counter

Wrap up

That’s all to implement or add a counter in a bash script. If you need any help or any suggestion feel free to comment down.

Leave a Reply