Using Counter in Bash with Practical Examples


Shell Scripting

Author: Omer Cakmak
Reviewer: Deepak Prasad

For Linux system experts, bash scripts are like toolboxes. They are scripts that will speed up the process for each job and eliminate repetitive jobs. Scripts eliminate duplicating the process manually, but repetitive work continues within the script. Counters help us determine how many times the process will be repeated.

 

How to Use Counter in Bash?

Counters are used for repetitive operations (loops) in Bash. Counter operators:

  • +,-
  • +=,-=
  • ++,--

We will talk about two methods used to count a number:

  • Let
  • Bash Arithmetic

We will demonstrate these methods in 3 different types of loops(for, while, until).

Arithmetic operations can be performed using double braces ((...)) and $((...)). In the let command, you can write " " with double quotes or without quotes.

Now let's examine the operators used with examples.

 

Using the + and - Counter Operators

Examples of the use of these operators are as follows:

for increment:

num=$((num+1))
((num=num+1))

let "num=num+1"

for decrement:

num=$((num-1))
((num=num-1))

let "num=num-1"

Example bash script for the for loop:

#!/bin/bash
for((num=0; num<5; num=num+1))
do
echo "num:"$num
done

Here, incrementing the counter was done with num=num+1. The script is written using let like this:

#!/bin/bash 
for((num=0; num<5;)) 
do 
        echo "num:"$num 
        let num=num+1 
done

Script output:

foc@fedora:/tmp$ ./counter.sh 
num:0
num:1
num:2
num:3
num:4

In this way, the num value is increased by one. By writing - instead of + statement, the opposite is done.

 

Using the += and -= Counter Operators

Examples of the use of these operators are as follows:

for increment:

num=$((num+=1)) 
((num+=1)) 

let "num+=1"

for decrement:

num=$((num-=1))
((num-=1))

let "num-=1"

Bash example for the += and -= counter operators in a while loop:

#!/bin/bash 
num=0 
while(($num<6)) 
do 
   	echo "num:" $num 
        ((num+=1)) 
done

Script output:

foc@fedora:/tmp$ ./counter.sh 
num: 0
num: 1
num: 2
num: 3
num: 4
num: 5

You can also write let num+=1 instead of ((num+=1)) value.

 

Using the ++ and -- Counter Operators

Examples of the use of these operators are as follows:

for increment:

num=$((num++))
((num++)) 

let "num++"

for decrement:

num=$((num--))
((num--))

let "num--"

In the Until loop, the counter is written like this:

#!/bin/bash
num=0
until [ $num -gt 7 ]
do
  	echo "num:"$num
        ((num++))
done

Instead of ((num++)) you can write let num++.

With this operator until loop output:

foc@fedora:/tmp$ ./counter.sh 
num: 0
num: 1
num: 2
num: 3
num: 4
num: 5
num: 6
num: 7

Counter continued to increase by one.

 

Summary

We explained the use of counters in Bash. We gave examples with frequently used loops. The help menu of the Let command will help in this regard:

foc@fedora:~$ let --help
let: let arg [arg ...]
    Evaluate arithmetic expressions.
    
    Evaluate each ARG as an arithmetic expression.  Evaluation is done in
    fixed-width integers with no check for overflow, though division by 0
    is trapped and flagged as an error.  The following list of operators is
    grouped into levels of equal-precedence operators.  The levels are listed
    in order of decreasing precedence.
    
    	id++, id--	variable post-increment, post-decrement
    	++id, --id	variable pre-increment, pre-decrement
    	-, +		unary minus, plus
    	!, ~		logical and bitwise negation
    	**		exponentiation
    	*, /, %		multiplication, division, remainder
    	+, -		addition, subtraction
    	<<, >>		left and right bitwise shifts
    	<=, >=, <, >	comparison
    	==, !=		equality, inequality
    	&		bitwise AND
    	^		bitwise XOR
    	|		bitwise OR
    	&&		logical AND
    	||		logical OR
    	expr ? expr : expr
    			conditional operator
    	=, *=, /=, %=,
    	+=, -=, <<=, >>=,
    	&=, ^=, |=	assignment
...

 

What's Next

I'm leaving a few posts about the loops we talked about while reviewing the counters:

 

References

stackoverflow.com - Adding Counter in shell script

 

Omer Cakmak

Omer Cakmak

He is highly skilled at managing Debian, Ubuntu, CentOS, Oracle Linux, and Red Hat servers. Proficient in bash scripting, Ansible, and AWX central server management, he handles server operations on OpenStack, KVM, Proxmox, and VMware. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment