For and While loop in Shell Scripts

Do not miss this exclusive book on Binary Tree Problems. Get it now for free.

Reading time: 30 minutes | Coding time: 10 minutes

In this article, we will understand how to use for and while loops in shell scripts which is widely used implementation technique.

We will cover the following topics:

  1. While Loops
  2. For Loops

Loops are used to implement same problem logic multiple times with slight variation.
Looping structures provided in Shell Scripts are while loop and for loop.

1. While loops

While loops are entry-controlled loops, i.e., they check the condition before entering the looping structure.

Syntax:

while [ (condition) ]
do 
    statement 1
    statement 2
    statement 3
    ...
done

Example:

#!/bin/bash
i=1
while [ $i -le 5 ]
do
	echo "$i";
	i=$(($i+1));
done

Shell Script code in vi editor

Here,

  • Condition: $i fetches the value of variable i and checks whether it is less than or equal to (specied by tag -le)
  • $(($i+1)): is used to increment the value of variable i

Output:

Infinite loop

Instead of specifying a condition, if : is specified, while goes on in an infinite loop.
To define exit in infinite loop in the code, break statement is used.
To exit the loop manually, one must click ctrl+c to kill the process or ctrl+z to stop the process.

Code:

#!/bin/bash
while :
do
	echo "infinite loop";
done

Shell code in vi-editor

Output:

2. For loops

For loops are entry-controlled looping structures that check the condition before entering the loop.

Syntax:

for iteration_variable in {set of space separated values}
do
    statement 1
    statement 2
    statement 3
    ...
done

Iteration variable accesses each element of set of values one by one and performs the task specified within the loop.

Example:

#!/bin/bash
for i in 1 2 3 4 5 6 7 8
do
	echo "This is loop $i";
done

Here, for loop access each numeric value and prints them in different lines.

Shell code on vi editor

Output:

For loop on list of items

For loop does necessary work on conditions.
It may simply access a set of values (which may be of different type as in Strings, integers, float values) one by one and performs defined task.

Code:

for i in Hello World "Hello World" Bye 
do
	echo "$i"
done

Shell code on vi editor

Here, for loop access each of the space separated string one by one and prints the iteration variable value in each loop.

Output:

For loops with increment

For loop may be specified for a range of values which increment with +1 jump.
Thus, if range is specified as 1-5, for loop will work for values 1,2,3,4 and 5.

Syntax:

for iteration_variable in {start..end}
do
    statements...
done

Code:

#!/bin/bash
for i in {1..10}
do
	echo "This is $i increment"
done

Shell code on vi editor

Here, iteration variable takes values 1,2,3,4,...9,10.

Output:

For loop with jump statements

For loops over range may also have a defined jump value:

Syntax:

for iteration_variable in {start..end..jump}
do
    statements...
done

Example: If for loop ranges from 0-10 and has a jump value of 2 then it access even numbers 0,2,4,6 and 10.

Code:

#!/bin/bash
for i in {1..10..2}
do
	echo "This is $i";
done

Shell code on vi editor

Output:

For loop with seq keyword

Range of values without increment can also be defined using the seq keyword.
Note: Use of seq keyword is not advised as seq keyword was supported in Bash version 3.0 or below.

Syntax:

for iteration_variable in `seq start end`

Code:

#!/bin/bash
for i in `seq 1 10`
do 
	echo "This is $i increment"
done

Shell code on vi editor

Output:

For loop in C/CPP style

Syntax:

for (( initialize_variable; condition; Increment ))

Note: Spacing in shell scripts are very important. Improper spacing may lead to shell interpreter not recognizing the command.

Code:

#!/bin/bash
for (( i=0; i<10; i++ ))
do
	echo "$i";
done

Shell code on vi editor

Output:

Infinite loops using for

Specifying no condition in C type for loops lead to infinite looping.

Code:

#!/bin/bash
for (( ; ; ))
do
	echo "Infinite loop";
done

Shell code on vi editor

Output:

Using break statement

Break keyword is used to exit the looping structures using a condition.
Generally, break condition is used to exit an infinite loop.

Code:

#!/bin/bash
i=1; #initialize variable to 1
for (( ; ; )) #infinite loop
do
	echo "$i"
	i=$(($i+1))
	if [ $i -eq 10 ]
	then
		break;
	fi
done

Shell code on vi editor

Here,

  • We initialize a variable with value 1.
  • We increment the variable with +1 in infinite loop
  • We exit the loop with break condition after variable gets a value 10.

Output:

Using continue statement

Continue condition is used to ignore the current looping statements and move on to the next looping condition.

Code:

#!/bin/bash
for i in 1 2 3 4 5
do 
	if [ $i -eq 3 ]
	then
		continue;
	fi;
	echo "$i";
done

Shell code on vi editor

Here, we ignore the looping statements having iteration value equal to 3.

Output:

References and Further Reading

Shell Scripting tutorials

Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.