Bash While LoopIn this topic, we have demonstrated how to use while loop statement in Bash Script. The bash while loop can be defined as a control flow statement which allows executing the given set of commands repeatedly as long as the applied condition evaluates to true. For example, we can either run echo command many times or just read a text file line by line and process the result by using while loop in Bash. Syntax of Bash While LoopBash while loop has the following format: The above syntax is applicable only if the expression contains a single condition. If there are multiple conditions to include in the expression, then the syntax of the while loop will be as follows: The while loop one-liner syntax can be defined as: There are some key points of 'while loop' statement:
How it worksThe while loop is a restricted entry loop. It means that the condition is checked before executing the commands of the while loop. If the condition evaluates to true, the set of commands following that condition are executed. Otherwise, the loop is terminated, and the program control is given to the other command following the 'done' statement. Bash While Loop ExamplesFollowing are some examples of bash while loop: While Loop with Single ConditionIn this example, the while loop is used with a single condition in expression. It is the basic example of while loop which will print series of numbers as per user input: Example Output While Loop with Multiple ConditionsFollowing is an example of while loop with multiple conditions in the expression: Example Output Infinite While LoopAn infinite loop is a loop that has no ending or termination. If the condition always evaluates to true, it creates an infinite loop. The loop will execute continuously until it is forcefully stopped using CTRL+C : Example We can also write the above script in a single line as: Output Here, we have used the built-in command (:) which always return true. We can also use the built-in command true to create an infinite loop just as below: Example This bash script will also provide the same output as an above infinite script. Note: Infinite loops can be terminated by using CTRL+C or by adding some conditional exit within the script.While Loop with a Break StatementA break statement can be used to stop the loop as per the applied condition. For example: Example Output According to the script, the loop is assigned to iterate for ten times. But there is a condition after eight times of iteration which will break the iteration and terminate the loop. The following output will be shown after executing the script. While Loop with a Continue StatementA continue statement can be used to skip the iteration for a specific condition inside the while loop. Example Output While Loop with C-StyleWe can also write while loop in bash script as similar as a while loop in C programming language. Example Output ConclusionIn this topic, we discussed how to use while loop statement in Bash to perform specific tasks. Next TopicBash Until Loop |