Looping Statements | Shell Script

11 Mar, 2022
Looping Statements in Shell Scripting: There are total 3 looping statements which can be used in bash programming  
while statementfor statementuntil statement
while statement
for statement
until statement
To alter the flow of loop statements, two commands are used they are,  
breakcontinue
break
continue
Their descriptions and syntax are as follows:  
while statement Here command is evaluated and based on the result loop will executed, if command raise to false then loop will be terminated Syntax  
 
 
for statement The for loop operate 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  
 
 
until statement The until loop is executed as many as times the condition/command evaluates to false. The loop terminates when the condition/command becomes true. Syntax  
 
 
Example ProgramsExample 1: Implementing for loop with break statement  
php
#Start of for loopfor a in 1 2 3 4 5 6 7 8 9 10do    # 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 10do    # 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 10while [ $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 10until [ $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 $COLORSdo    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 truedo  # Command to be executed  # sleep 1 indicates it sleeps for 1 sec  echo "Hi, I am infinity loop"  sleep 1done
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=nwhile [ "$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? " CORRECTdone
Output:
$bash -f main.sh
Enter your name:Ironman
Is Ironman correct? n
Enter your name:Spiderman
Is Spiderman correct? y
ashutosh450
harikamaryala
Shell
Shell Script
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.