Module-5 How to use for loops in Bash scripting

Table of Contents

Introduction

In this article we will be covering the ways in which we can use for loops in the bash scripting along with practical examples to try it out while reading the article. This serves as both a Tutorial  as well as a quick reference guide on For loops in Bash Scripting 

Understanding the Syntax to follow while using For loops in bash scripts

In the following script we have an example of a simple For loop to understand how to use this in the bash scripts. We are iterating in the numbers available from 6 through 9 and printing them using the variable substitution in the echo command

[root@discoveringsystems for-loops]# cat basic-for-loop
#!/bin/bash


for numbers in 6 7 8 9
do

  echo "digits $numbers"

done
#executing the script

[root@discoveringsystems for-loops]# ./basic-for-loop
digits 6
digits 7
digits 8
digits 9

How to use numbers in For loop

In this section we will see various ways we can use numbers in Bash for loops for iteration

How to use range in for loops

In the following example we were using the range to do the same thing we did in the earlier example , we are printing numbers 6 through 9 in both normal way and with the range syntax

[root@discoveringsystems for-loops]# cat basic-for-loop
#!/bin/bash


for numbers in 6 7 8 9
do

  echo "digits $numbers"

done

#or

for numbers in {6..9}
do

  echo "digits $numbers"

done
#executing the script 

[root@discoveringsystems for-loops]# ./basic-for-loop
digits 6
digits 7
digits 8
digits 9
digits 6
digits 7
digits 8
digits 9

How to use range with steps

In this example , we are using the same range concept which we discussed in the earlier example , however we are asking it to iterate in the steps of 2. The range we set here is 1 through 10 , in that we were asking the script to iterate in steps of 2. So it starts with 1 and then applies steps of 2 to get these 1, 3,5,7,9

[root@discoveringsystems for-loops]# cat basic-for-loop
#!/bin/bash


for numbers in {1..10..2}
do

  echo "digits $numbers"

done
#executing the script 

[root@discoveringsystems for-loops]# ./basic-for-loop
digits 1
digits 3
digits 5
digits 7
digits 9

How to use 3 parameter loop control expression

In this example we are using 3 parameter loop control expressions for the numbers in Bash for loops.

  • Here we are initializing the num ( num=1) 
  • then setting the condition that the loop should continue until the condition that the num is either lesser than or equal to 7 ( num<=7), 
  • Then for each iteration we are incrementing it with count of 1 (num++)

This conditional , made the for loop with echo command to print numbers from 1 through 7 as per the condition 

[root@discoveringsystems for-loops]# cat basic-for-loop
#!/bin/bash


for (( num=1; num<=7; num++ ))
do

  echo "digits $num"

done
#executing the script 

[root@discoveringsystems for-loops]# ./basic-for-loop
digits 1
digits 2
digits 3
digits 4
digits 5
digits 6
digits 7

How to use Strings in For loops

In this example , we were storing the set of strings with spaces in between into a single variable named days , then we used that variable to iterate in the for loop and printed it

[root@discoveringsystems for-loops]# cat basic-for-loop
#!/bin/bash

echo "Days in the Week"

days="mon tues wed thurs fri sat sun"

for day in $days
do
  echo "$day"

done
#executing the script 

[root@discoveringsystems for-loops]# ./basic-for-loop
Days in the Week
mon
tues
wed
thurs
fri
sat
sun

How to use output from other commands in the for loops

We have 10 files in the present working directory , we are using this output from the “ls -1tr |grep -i file “ in the for loop to iterate through the file names to write something into it

[root@discoveringsystems for-loops]# ls -1tr | grep -i file
file1.txt
file2.txt
file3.txt
file4.txt
file6.txt
file5.txt
file7.txt
file8.txt
file9.txt
file10.txt

we were able to write a script which writes content of into each and every file we are iterating through

[root@discoveringsystems for-loops]# cat basic-for-loop
#!/bin/bash

for file in $(ls -1tr | grep -i file)
do
  echo "content of $file" > $file

done

After executing the script we were able to see the things written into each and every file

#executing the script 

[root@discoveringsystems for-loops]# ./basic-for-loop
[root@discoveringsystems for-loops]#
[root@discoveringsystems for-loops]#  grep -i file *

file10.txt:content of file10.txt
file1.txt:content of file1.txt
file2.txt:content of file2.txt
file3.txt:content of file3.txt
file4.txt:content of file4.txt
file5.txt:content of file5.txt
file6.txt:content of file6.txt
file7.txt:content of file7.txt
file8.txt:content of file8.txt
file9.txt:content of file9.txt

How to use array in the for loops

In this example , we created a array which has strings of first 3 integers and then used it in the for loop to iterate through it , the example also has the syntax to use while using the array in the for loop

[root@discoveringsystems for-loops]# cat basic-for-loop
#!/bin/bash

simple_array=('one' 'two' 'three')

for numbers in "${simple_array[@]}"
do
  echo "num is $numbers "

done
#executing the script 

[root@discoveringsystems for-loops]# ./basic-for-loop
num is one
num is two
num is three

How to create an infinite for loop in bash scripting

Here in this example , we have used an infinite for loop in bash script and for each iteration we have incremented the count. Since it is infinite loop , we need to hit ctrl +c to stop the script after running it on foreground

[root@discoveringsystems for-loops]# cat basic-for-loop
#!/bin/bash

count=1

for (( ; ; ))
do
  echo "$count"
  ((count++))

done
#executing the script and we need to use ctrl +c to stop as its an infinite loop 

[root@discoveringsystems for-loops]# ./basic-for-loop
.
.
.
.
57854
57855
57856
57857
^C
[root@discoveringsystems for-loops]# 

How to use break statement in the bash for loop

While iterating through the set of files or set of items , let’s say we got the exact file or item we were looking for , in that case it might be not required to iterate through the rest of the items and then exit the for loop. To exit the for loop after the condition is met through the conditionals we can use the break statement

In the following script we are iterating through file1.txt through file10.txt , we are going to exit the for loop as soon as we found the file5.txt

[root@discoveringsystems for-loops]# ls -1tU | grep -i file
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt
file6.txt
file7.txt
file8.txt
file9.txt
file10.txt
[root@discoveringsystems for-loops]# cat basic-for-loop
#!/bin/bash


for file in $( ls -1tU | grep -i file )
do
  echo "$file"
  if [ "$file" == "file5.txt" ]
  then
    break
  fi

done
#executing the script 

[root@discoveringsystems for-loops]# ./basic-for-loop
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt

How to use the continue statement in bash for loop

We used the break statement in for loop for exiting the for loop after a condition is met. Let’s say we have a requirement to delete all files except a particular file in the iteration list when a condition is met. When we have such a requirement we can use continue statement in for loops

In my example script , I am iterating through the same 10 files file1.txt through file10.txt and i would delete each and every file except the file5.txt , so when I do each iteration I will delete the files , but when they match the file5.txt . I used continue statement to skip further set of command execution part of that for loop iteration and jump to next iteration

# we can see all the 10 files present initially

[root@discoveringsystems for-loops]# ls -1tU | grep -i file
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt
file6.txt
file7.txt
file8.txt
file9.txt
file10.txt
# in the script we matched and skipped the file5.txt from getting deleted with the help of continue statement 

[root@discoveringsystems for-loops]# cat basic-for-loop
#!/bin/bash


for file in $( ls -1tU | grep -i file )
do

  if [ "$file" == "file5.txt" ]
  then
    continue
  fi
  rm -f $file
  echo "$file is deleted"
  
done
#executing the script and we can see the file5.txt alone got skipped. 

[root@discoveringsystems for-loops]# ./basic-for-loop
file1.txt is deleted
file2.txt is deleted
file3.txt is deleted
file4.txt is deleted
file6.txt is deleted
file7.txt is deleted
file8.txt is deleted
file9.txt is deleted
file10.txt is deleted
# as a result we only see the file5.txt remaining , rest others are deleted 

[root@discoveringsystems for-loops]# ls -1tU | grep -i file
file5.txt

Leave a Comment

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