Open In App
Related Articles

Looping Statements | Shell Script

Improve Article
Improve
Save Article
Save
Like Article
Like

Looping Statements in Shell Scripting: There are total 3 looping statements that can be used in bash programming 

  1. while statement
  2. for statement
  3. until statement

To alter the flow of loop statements, two commands are used they are,  

  1. break
  2. continue

Their descriptions and syntax are as follows: 

while statement

Here the command is evaluated and based on the resulting loop will execute, if the command is raise to false then the loop will be terminated that 

while <condition>
do

<command 1>
<command 2>
<etc>
done

for statement

The for loop operates on lists of items. It repeats a set of commands for every item in a list. 
Here var is the name of a variable and word1 to wordN are sequences of characters separated by spaces (words). Each time the for loop executes, the value of the variable var is set to the next word in the list of words, word1 to wordN. 

Syntax:  

for <var> in <value1 value2 ... valuen>
do
<command 1>
<command 2>
<etc>
done

until statement

The until loop is executed as many times as the condition/command evaluates too false. The loop terminates when the condition/command becomes true. 

Syntax: 

until <condition>
do

<command 1>
<command 2>
<etc>
done

Example Programs

Example 1:

Implementing for loop with break statement 

php




#Start of for loop
for a in 1 2 3 4 5 6 7 8 9 10
do
    # if a is equal to 5 break the loop
    if [ $a == 5 ]
    then
        break
    fi
    # Print the value
    echo "Iteration no $a"
done


Output:

$bash -f main.sh
Iteration no 1
Iteration no 2
Iteration no 3
Iteration no 4

Example 2: 

Implementing for loop with continue statement  

php




for a in 1 2 3 4 5 6 7 8 9 10
do
    # if a = 5 then continue the loop and
    # don't move to line 8
    if [ $a == 5 ]
    then
        continue
    fi
    echo "Iteration no $a"
done


Output:

$bash -f main.sh
Iteration no 1
Iteration no 2
Iteration no 3
Iteration no 4
Iteration no 6
Iteration no 7
Iteration no 8
Iteration no 9
Iteration no 10

Example 3: 

Implementing while loop  

php




a=0
# -lt is less than operator
 
#Iterate the loop until a less than 10
while [ $a -lt 10 ]
do
    # Print the values
    echo $a
     
    # increment the value
    a=`expr $a + 1`
done


Output: 

$bash -f main.sh
0
1
2
3
4
5
6
7
8
9

Example 4: 

Implementing until loop 
 

php




a=0
# -gt is greater than operator
 
#Iterate the loop until a is greater than 10
until [ $a -gt 10 ]
do
    # Print the values
    echo $a
     
    # increment the value
    a=`expr $a + 1`
done


Output: 
 

$bash -f main.sh
0
1
2
3
4
5
6
7
8
9
10

Note: Shell scripting is a case-sensitive language, which means proper syntax has to be followed while writing the scripts.

Example 5:

PHP




COLORS="red green blue"
 
# the for loop continues until it reads all the values from the COLORS
   
for COLOR in $COLORS
do
    echo "COLOR: $COLOR"
done


Output:

$bash -f main.sh
COLOR: red
COLOR: green
COLOR: blue

Example 6:

We can even access the positional parameters using loops

We execute a shell script named sample.sh using three parameters

Script Execution: main.sh sample1 sample2 sample3

We can access above three parameters using $@

PHP




echo "Executing script"
   
# the script is executed using the below command
# main.sh sample1 sample2 sample
# where sample1, sample2 and sample3 are the positional arguments
# here $@ contains all the positional arguments.
 
for SAMPLE in $@
do
    echo "The given sample is: $SAMPLE"
done


Output:

$bash -f main.sh sample1 sample2 sample3
Executing script
The given sample is sample1
The given sample is sample2
The given sample is sample3

Example 7:

Infinite loop

PHP




while true
do
  # Command to be executed
  # sleep 1 indicates it sleeps for 1 sec
  echo "Hi, I am infinity loop"
  sleep 1
done


Output:

$bash -f main.sh
Hi, I am infinity loop
Hi, I am infinity loop
Hi, I am infinity loop
.
.
.
.
It continues

Example 8:

Checking for user input

PHP




CORRECT=n
while [ "$CORRECT" == "n" ]
do
  # loop discontinues when you enter y i.e.e, when your name is correct
  # -p stands for prompt asking for the input
  read -p "Enter your name:" NAME
  read -p "Is ${NAME} correct? " CORRECT
done


Output:

$bash -f main.sh
Enter your name:Ironman
Is Ironman correct? n
Enter your name:Spiderman
Is Spiderman correct? y

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 31 Jul, 2023
Like Article
Save Article
Previous
Next
Similar Reads