Linux Bash While Loop Tutorial with Examples


Linux bash provides a lot of mechanisms to make the system administrator’s life easier. These features are similar to the programming language features like variables, decisions, loops, etc. In this tutorial, we will look loops which can be implemented with for and while we have already examined for loops in the following tutorial.

Bash For Loop Examples

What is Loop?

Loop is a mechanism where given items iterated one by one and given statement executed repeatedly. For example, we want to print numbers to the console from 1 to 10 writing 10 times print statement is not an efficient way. So we can use a loop and iterate from 1 to 10 and print the current item.

Bash while Loop Syntax

The bash while loop has a simple syntax. We will define while  and the condition and then we put code we want to execute in every iteration between do  and done statements. CODE can be more than one line.

while CONDITION
do
   CODE
   CODE
done

Count and Print From 0 To Specified Number

Now we will do a simple example. We want to count from 0 to the specified number which is 10 in this example. We will also use some comparison statements provided by bash. Comparison statements will compare whether given conditions are met in each step.

#!/bin/bash 
x=1 
while [ $x -le 10 ] 
do 
   echo $x 
   ((x += 1)) 
done
Count and Print From 0 To Specified Number
Count and Print From 0 To Specified Number

Bash while Single Line Loop

We can create w loop in a single line by using bash ; separator. This will create multiple statements in a single line. As we can see we created a single line while loop but separated while , do and done .

$ while true ; do echo "This is infinite loop."; done

Bash while Infinite Loops

There is a special loop example which is named the infinite loop. As its name states, these loops do not end by itself. We can end this loop using external ways like the cancel process by sending process signals. This loop can be useful if we need to check some values every time. If we set the condition always TRUE logic value this will be an infinite loop.

$ while true ; do echo "This is infinite loop."; done
Infinite Loops
Infinite Loops

Conditional Break During Loop with Break Statement

Some times we may need to break the current loop if some condition is met. This will end the loop even previously given condition is not met. We will use the break mechanism to exit while loop. In this example, if the sum of given values is greater than 10 we will break the loop.

#!/bin/bash 
x=1 
sum=0 
while [ $x -le 10 ] 
do 
   echo $x 
   ((sum+=$x)) 
   ((x += 1)) 
   if [ $sum -ge 10 ]; then break; fi 
done
Conditional Break During Loop with Break Statement
Conditional Break During Loop with Break Statement

Skip To The Next Step with Continue Statement

In general, we use some code for each iteration to process something. But in some cases, we may need to skip given steps and done process. We can use continue statement to skip the next step. We should provide conditions to skip the given steps. In this example, we will skip odd numbers.

#!/bin/bash 
x=1 
 
while [ $x -le 10 ] 
do 
   o=$(( $x % 2 )) 
   if [ $o -eq 1 ]; then continue ; fi 
   echo $x 
   ((x += 1)) 
done

LEARN MORE  Foreach Usage In PHP, JavaScript, Java, C#, Python Programming Languages with Examples

Leave a Comment