Shell Scripting While Loop

While Loop

Syntax for while loop:

           while [ condition ]
           do
                 statement1
                 statement2
           done

While loop example:

#!/usr/bin/env bash            
                
# While loop example                       
COUNTER=0       
                
while [ $COUNTER -le 5 ]                      
do                      
 echo "COUNT = $COUNTER"       
                
 ((COUNTER++))       
                
done       
                               
echo "while loop completed."       

Output:

COUNT = 0
COUNT = 1
COUNT = 2
COUNT = 3
COUNT = 4
COUNT = 5
while loop completed.

Also, see the example code shell-scripting-examples in our GitHub repository. See complete examples in our GitHub repositories.

Follow us on social media