The Syntax of a while loop in BASH Scripting

while [ condition ];
do
# statements
# commands
done



If the condition is true then the commands inside the while block are executed and are iterated again after checking the condition. Also if the condition is false the statements inside the while block are skipped and the statements after the while block are executed. 

Bash Scripting – While Loop

A while loop is a statement that iterates over a block of code till the condition specified is evaluated to false. We can use this statement or loop in our program when do not know how many times the condition is going to evaluate to true before evaluating to false.  

Table of Content

  • The Syntax of a while loop in BASH Scripting
  • Looping Example using while statement in Linux
  • Reading a file with a while loop  
  • Infinite while loop
  • While loop to iterate for a fixed number of times
  • Read the command-line argument with getopts options
  •  C-style while loop
  • While loop to perform operations on a file
  • Writing to a file using a while loop
  • Break and continue Using while Loop
  • Frequeltly Asked Questions

Similar Reads

The Syntax of a while loop in BASH Scripting

while [ condition ];do # statements # commandsdone...

Looping Example using while statement in Linux

First, we create a text file using a text editor in Linux, in this case we are using `vim` Text Editor....

Reading a file with a while loop

We can read a file with a while loop in BASH.  By parsing certain parameters to the while loop condition, we can iterate over the file line by line or by other groups like characters or words....

Infinite while loop

To create an infinite loop using a while loop statement. We don’t need to put any condition in the while loop and hence the loop iterates infinitely. The below is the example of an infinite while loop:...

While loop to iterate for a fixed number of times

We can use a while loop to iterate over a fixed number of times, we can set the condition to be -le or less than equal to a number, and the loop will iterate till the iterator is less than or equal to the number provided in the condition. Also, we need to increment the iterator manually so to keep the loop iterator ticking, or else the loop will go on forever....

Read the command-line argument with getopts options

We can use get ops options to read the input from the command line and if there are multiple arguments, we can check them and parse them one by one using a while loop. getopts is a tool to get user input from the command line, We can have multiple options to parse from the command line, and using getopts and while loops, we can make a professional-looking user input program....

C-style while loop

We can use C-styled while loop in BASH, the variables need to be evaluated with BASH style but the overall syntax feels like C. We can use the operators like <,>,<= and so on in the condition of the while loop and hence it is called like the C-styled while loop....

While loop to perform operations on a file

We can perform operations on a file, like reading only a particular part of a file. If we have columns that are formatted in a particular fashion, we can use variables to assign them and print them one row data time....

Writing to a file using a while loop

We can write to a file by user input in a while loop. We can use the while loop to iterate till we manually exit out of the loop using CTRL + D by saving changes to the file or by CTRL + C for avoiding writing to the file. We use the read command to input the text from the command line and parse it to the file....

Break and continue Using while Loop

Break and continue are essential in a program that deals with loop statements as they control the flow of the loop statements without any explicit conditions....

Frequeltly Asked Questions

1. What is a `while` loop in Bash scripting?...

Conclusion

In this article we discussed the BASH scripting while loop which proves to be a versatile tool for executing a block of code repeatedly based on a specified condition. The basic syntax involves defining the condition within square brackets, allowing for flexible iterations. The article explores diverse applications of while loops, including reading from files, creating infinite loops, fixed iterations, parsing command-line arguments, and utilizing break and continue statements. From C-style syntax to interactive file writing, each example demonstrates the loop’s adaptability in addressing a range of scripting needs. This comprehensive overview underscores the while loop’s significance in BASH scripting, providing a robust mechanism for controlled repetition and efficient code execution....