Breaking out of a loop in Bash
Using loops is a common task in any programming or scripting language. While using a loop, sometimes we need to stop it under a predefined condition.
Like other programming and scripting languages, Bash uses the keyword break to stop any loop.
This article will show you how to stop the execution of a loop. Moreover, we will discuss the topic with necessary examples and explanations to make the topic easier to understand.
We will look at the three most commonly used loops: while, for, and until. Let's start with each one.
Break out of while loop in Bash
You can use the keyword break in a while loop. This stops the execution of the while loop under a specified condition.
Take a look at the following example:
i=0
while [[ $i -lt 15 ]]
do
if [[ "$i" == '4' ]]
then
echo "Number $i! We are going to stop here."
break
fi
echo $i
((i++))
done
echo "We are stopped!!!"
In the example shared above, when the value of i is equal to 4, we stopped the while loop.
After executing the above Bash script, you will get an output as shown below:
0
1
2
3
Number 4! We are going to stop here.
We are stopped!!!
Break out of a for loop in Bash
The keyword break
can also be used to stop a for loop under certain conditions. To do this, see the following example:
for i in {1..15}
do
if [[ $i == '5' ]]
then
echo "Number $i! We are going to stop here."
break
fi
echo "$i"
done
echo "We are stopped!!!"
In the example shared above, when the value of i is equal to 5, we stopped the for loop.
After executing the above Bash script, you will get the following output:
1
2
3
4
Number 5! We are going to stop here.
We are stopped!!!
Break out of until loop in Bash
There is another popular loop in Bash until
which can also be stopped by the keyword break. To stop until, you can follow the example shared below:
i=0
until [[ $i -gt 15 ]]
do
if [[ $i -eq 5 ]]
then
echo "Number $i! We are going to stop here."
break
fi
echo $i
((i++))
done
echo "We are stopped!!!"
In the example shared above, when the value of i is equal to 5, we will stop the until loop.
After executing the above Bash script, you will get the output as follows:
0
1
2
3
4
Number 5! We are going to stop here.
We are stopped!!!
We can choose any of the above methods depending on the loop.
All the codes used in this article are written in Bash. It will only work in Linux Shell environment.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Check if input parameter exists in Bash
Publish Date:2025/03/21 Views:191 Category:OPERATING SYSTEM
-
When we create a Bash script, we may want to use parameters in our script to run successfully. Therefore, we need to create a script to check the number of input parameters used by the user in the script. All of this prevents unexpected beh
Bash configuration files in macOS
Publish Date:2025/03/21 Views:185 Category:OPERATING SYSTEM
-
In this article, we will discuss how to create, delete, and edit bash profiles in macOS. Create .bash_profile To create .bash_profile, open a terminal and move to your home directory using the following command. cd ~/ Now you can create usi
eval command in bash script
Publish Date:2025/03/21 Views:87 Category:OPERATING SYSTEM
-
This article is about using strings as commands in Bash scripts. For this purpose, the eval command is used. Eval Command in Bash Scripts In some Bash scripts, you have to create a string using variables or input values (for example)
Exit Bash Script
Publish Date:2025/03/21 Views:99 Category:OPERATING SYSTEM
-
This article provides a brief introduction to Bash scripting and discusses exiting a Bash script when an error occurs. It further discusses the limitations and benefits of Bash scripting. What is Bash Scripting A computer script/program tel
timeout command in Bash
Publish Date:2025/03/21 Views:105 Category:OPERATING SYSTEM
-
This article is a simple guide to setting a timeout for a specific program using the timeout command from GNU's coreutils package in Bash. timeout command in Bash In many cases, such as fetching data from a server or running a function or p
Start a new terminal session in Bash
Publish Date:2025/03/21 Views:51 Category:OPERATING SYSTEM
-
In various situations when using Bash or another shell, you may need to run a script or program in a new terminal instance or in another tab in the same terminal. Opening a new terminal instance or tab from within Terminal is simple; we'll
Checking Syntax in Bash
Publish Date:2025/03/21 Views:89 Category:OPERATING SYSTEM
-
This article discusses methods for checking Bash scripts for syntax problems without running the script. Syntax errors are caused by some grammatical or spelling mistakes in the code. In modern compilers of other programming languages, the
Run batch (.bat) files in CMD
Publish Date:2025/03/20 Views:155 Category:OPERATING SYSTEM
-
This article will show you how to use CMD to run a batch file.bat. There are three ways to run a batch file. Let us discuss them in the following sections. Run batch (.bat) files in CMD by directly clicking on them This way you can just go
Changing CMD text color using batch script
Publish Date:2025/03/20 Views:172 Category:OPERATING SYSTEM
-
This article will first discuss the basic concepts of batch scripts or batch files. After introducing the Batch script, let's discuss how to use the Batch script to change the text color of CMD every second. Batch script or file A batch scr