Updated April 14, 2023
Definition of Linux While Loop
Linux while loop command is a control flow condition that will help to run the command to execute it repeatedly based on a certain condition. The execution must continue until the condition is validated to be true. The while condition started with the keyword “while” and proceeded by conditional expressions or statements. Linux while loop command is a control flow condition that will help to run the command to execute it repeatedly based on a certain condition.
This article will help you to understand the basics of while loop condition in Linux Shell scripting. Conditional looping statements – Break and continue statements using while loop is also very well explained in this article.
Syntax:
The basic syntax of using while loop is given below:
while [condition]
do
[ Statements ]
done
Here from statements, will be executed continuously until the condition that is mentioned in the braces becomes true. The condition or argument of a while loop can also be a boolean parameter. There might be chances when the while loop run in an infinite loop when the condition passed is never false.
How does While Loop Work in Linux?
While the statement always starts with ‘while’ keyword and then it is followed by conditions and commands. There might be n number of commands that can be executed one by one.
while [condition]
do
command 1
command 2
.
.
command n
done
Here from command 1 to command n, will be executed continuously until the condition that is mentioned in the braces becomes true. The condition or argument of a while loop can also be a boolean parameter. There might be chances when the while loop run in an infinite loop when the condition passed is never false.
The condition given in while loop will be executed first before the commands. Only when the condition is true, the commands below are executed. Else, the commands will not be executed and thus the while loop is terminated.
Examples of Linux While Loop
Below are a few scenarios that are used in general shell scripting using a while loop.
1. To Print a String 5 Times in the Output
We can print a number or a string repeatedly n number of times in a loop by using while loop.
Command:
x=1
while [ $x -le 5 ]
do
echo "number is $x"
x=$(( $x + 1 ))
done
Output:
The result of the above snippet is given below:
- To print a string ‘n’ number of times in a liner code by while loop. We can do this by:
Command:
x=1;
while [ $x -le 5 ];
do
echo "Hello to all $x times"
(( x++ ));
done
Output:
The result of the above snippet is given below:
2. While Loop to Find a Factorial of A Number
To run a while loop in a shell script or a bash script, we can write a code as below.
Let us create a shell script ‘factorial.sh’ to find the factorial of a given number. To compile this script, we would use while loop.
Command:
cat factorial.sh
counter=$1
factorial=1
while[ $counter -gt 0 ]
do
factorial=$(( $factorial * $counter ))
counter=$(( $counter - 1 ))
done
echo $factorial
Output:
The result of above snippet is given below:
3. Infinite While Loop Condition
Whenever the condition parameter in while loop is empty, the loop will repeat itself infinite times.
Command:
cat infinite.sh
while :
do
echo "infinite loops [ Press Ctrl+C to break the loop ]"
done
4. Conditional While Loop
A loop might repeat continuously all the time when the condition parameter is not met. When a loop will execute repeatedly without stopping for n number of times, such a loop is called an infinite loop. To manage the loop, there are two conditional statements used in while loop. They are:
- break
- continue
- Break Statement:
The break statement helps to stop the execution of the while loop, only when it completes executing all the line of codes till the break statement is mentioned. It will then jump to the end of the while loop.
We can come out of the while loop using the ‘BREAK’ statement. Whenever we would want to exit early from the while loop without condition satisfying, we can do so by a break statement.
Syntax of break statement in while loop is given below:
Syntax:
while [condition]
do
Statement 1
Statement 2
if (condition)
then
break
fi
Statement n
done
5. Continue Statement
The continue statement and break command are quite similar to each other other than that may cause the present iteration of the loop to end, instead of the whole loop. This continue statement is used when any issue occur but we would try to executing the other iteration in the loop.
To continue the iteration in while loop, we can use ‘CONTINUE’ Statement. Syntax of break statement in while loop is given below:
Syntax:
while [ condition ]
do
Statement 1
Statement 2
if (condition)
then
continue
fi
Statement n
done
6. Nesting While Loops
We can use while loop in another while loop and this process is called nesting of while loop. Syntax of nesting while loop is given below:
Syntax:
while [ condition 1 ]
do
statement
while [ condition 2 ]
do
statement
done
statement n
done
Example:
cat nesting_loop.sh
a=0
while [ "$a" -lt 5 ] #this is loop1
do
b="$a"
while [ "$b" -ge 0 ] #this is loop2
do
echo -n "$b"
b=`expr $b-1`
done
echo
a=`expr $a+1`
done
Output:
The above code will give you the below output. You should see the way ‘echo n’ is working here in nesting while loop.
Conclusion
There are many loop conditions in Linux or Unix Shell scripting. Out of them, While loop is very more useful and easy to use in loops. The above article will, therefore, help you to understand the usage of a while loop. Examples of the usage of while loop condition is also mentioned for easy understanding.
Recommended Articles
We hope that this EDUCBA information on “Linux While Loop” was beneficial to you. You can view EDUCBA’s recommended articles for more information.