How to break out of a while loop in Bash

Loops are an extremely useful tool for performing repetitive tasks, not only in Bash scripts, but in all other programming languages. This allows us to write a task at once (which must be executed multiple times) and wrap it in any loop we want so that the specified task can be executed repeatedly. Each programming language uses different loops, that is, several types of loops can be used with each programming language. Of all the types, the “for” and “while” loops are most commonly used.

The main difference between executing a for loop and a while loop is that the first loop specifies the increment or decrement variable using a loop, while the second specifies the variable after the task to be repeated. While loops seem to be more syntax-friendly to programmers.

The concept of infinite loops in all programming languages ​​is also very common, that is, a loop that never ends and its state is always evaluated as “true”. Sometimes these loops are written by programmers by accident, but there are situations when such loops are written intentionally. In any case, there may be certain conditions under which we want this endless loop to break.

Apart from the scenario we discussed above, there are times when we intentionally create end loops that we want to run based on a certain condition in which we want the normal flow of that loop to break. For both scenarios, there must be a proper mechanism in which we can break the loop whenever a certain specified condition is met.

We can achieve this goal by using a break statement on our loops, whether they are finite or infinite. Since the while loop is one of the most commonly used loops in any programming language, we will see how we can break out of the while loop in Bash in Linux Mint 20 by sharing an example Bash script with you.

Sample Script to Exit Bash While Loop in Linux Mint 20

To demonstrate the use of the “break” command in Bash, you must create a Bash file in your home directory. In our case, we named it “BreakWhile.sh”. You can also have any other name for this Bash file. After creating this file, you must open it in any text editor and then write the script shown in the following image:

In this script, we have defined a variable named “number” and initialized it with the value “1”. Then we have a “while” loop, the iteration condition of which is that the value of the variable “number” must be less than 10, that is, this loop will continue to repeat until the value of the variable “number” is less than 10 Then, in the do-done block, we have an “if” statement, the condition of which is that whenever the value of the variable “number” equals “9”, our “while” loop will be interrupted. Otherwise, it will continue to work. Then we just printed out the value of the variable number for each iteration of our while loop. Finally, we have increased the value of our “numeric” variable, that is, the value of our “numeric” variable will be greater than one after each iteration of our “bye” loop.

To test this situation, we have to execute the Bash script we just created using the command shown below. However, before running this command, you must make sure to save your Bash script file.

$ bash BreakWhile.sh

You can easily see that the numbers printed on the terminal are from 1 to 8, and the number “9” is not printed, which means that our “while” loop has successfully completed with the “break” command.

Output

This article shows a fairly simple example of breaking out of a while loop in Bash in Linux Mint 20. The same Bash script can be executed on any other Linux distribution of your choice and will give the same results. In addition, you can even use this “break” statement with a “for” loop or any other loop in Bash to interrupt its normal flow. This statement is extremely useful, especially if you have a special case in your program for which you do not want your program to continue its normal execution, or you may even want the control of your program to take a completely different path of execution.

However, the important thing to keep in mind here is that we just wanted to give you an edge when using a break statement with a while loop in Bash in Linux Mint 20. This is why we just created simple Bash. script for outputting to the terminal some trial numbers that are less than 10, with the exception of the number “9”. But this does not mean that the break statement is only used in such simple scenarios. You can create even more sophisticated programs to test the effectiveness of a break statement using a while loop in Bash in Linux Mint 20. Hopefully, with this article, you can easily create any bash script of your choice using break.

Sidebar