break command in Linux with examples
Last Updated :
15 Sep, 2023
The break
command in Linux is primarily used within loops to exit or terminate the loop prematurely based on certain conditions. It offers a quick and convenient way to escape from a loop's execution, whether it's a for
loop, a while
loop, or a nested loop structure. It can also take one parameter i.e.[N]. Here n is the number of nested loops to break. The default number is 1.
Syntax of `break` command in Linux
break [n]
When the break
command is encountered within a loop, the loop immediately terminates, and the program flow continues with the statement immediately following the loop.
Example 1: Using break statement in for loop
#!/bin/bash
for i in {1..5}; do
echo $i
if [ $i -eq 3 ]; then
break
fi
done
echo "For loop terminated"
In this example, the break
statement is triggered when the value of i
becomes 3. As a result, the loop terminates.

Example 2: Using break statement in while loop
If we want to print numbers from 1 to 10 using a while
loop, but halt the loop when the number 7 is reached.
#!/bin/bash
count=1
while [ $count -le 10 ]; do
echo $count
if [ $count -eq 7 ]; then
break
fi
((count++))
done
echo "While loop terminated"
In this case, the loop iterates through the numbers, but the break
statement comes into play when count
becomes 7. As a result, the loop terminates early.

Example 3: Using break statement in until loop
In this example we will printing numbers from 1 to 5 using an until
loop but stopping when the number 4 is encountered.
#!/bin/bash
number=1
until [ $number -gt 5 ]; do
echo $number
if [ $number -eq 4 ]; then
break
fi
((number++))
done
echo "Until loop terminated"
In this case the loop continues until number
becomes greater than 5. However, the break
statement intervenes when number
reaches 4, leading to an early termination.

break --help : It displays help information.
We can access this information using the break --help
command. Simply execute the following in our terminal.
break --help

Conclusion
In this aarticle we have discussed about `break` command in Linux which exits loops based on conditions. It's versatile across loop types, including nested ones, and uses syntax like break [n]
. Whether ending for
, while
, or until
loops, break
streamlines scripting. Plus, break --help
offers easy access to help information.
Similar Reads
autoconf command in Linux with examples autoconf command is used in Linux to generate configuration scripts. Generate a configuration script from a TEMPLATE-FILE if given, or from âconfigure.acâ if present, or 'configure.in'. The output is sent to the standard output if TEMPLATE-FILE is given, otherwise, it is sent into âconfigureâ. To us
1 min read
autoheader command in Linux with Examples autoheader command in Linux is used to create a template file of C â#defineâ or any other template header for configure to use. If the user will give autoheader an argument, it reads the standard input instead of reading configure.ac and also writes the header file to the standard output. This comma
3 min read
automake command in Linux with Examples automake is a tool used for automatically generating Makefile.in files compliant with the set GNU Coding Standards. autoconf is required for the use of automake. automake manual can either be read on-line or downloaded in the PDF format. More formats are also offered for download or on-line reading.
1 min read
autoreconf command in Linux with examples autoreconf is a Autotool which is used to create automatically buildable source code for Unix-like systems. Autotool is a common name for autoconf, automake, etc. These all together are termed as Autotools. Important Points: Provides Portability of source code packages by automatic buildable capabil
2 min read
AWK command in Unix/Linux with examples Awk is a scripting language used for manipulating data and generating reports. The awk command programming language requires no compiling and allows the user to use variables, numeric functions, string functions, and logical operators. Awk is a utility that enables a programmer to write tiny but eff
8 min read
banner command in Linux with examples The 'banner' command in Linux is a simple yet powerful utility used to display text in large ASCII characters on the terminal. This command can be particularly useful for creating prominent messages or headings within scripts and outputs.Syntax of the 'banner' Commandbanner textbanner 'Command' Exam
3 min read
basename Command in Linux with examples The 'basename' command in Linux is a fundamental utility used in file manipulation and script writing. It simplifies file paths by stripping directory information and optional suffixes from file names. Here a detailed overview of the 'basename' command, including its syntax, options, and practical u
3 min read
batch command in Linux with Examples batch command is used to read commands from standard input or a specified file and execute them when system load levels permit i.e. when the load average drops below 1.5. Syntax: batch It is important to note that batch does not accepts any parameters. Other Similar Commands: atq: Used to display th
1 min read
bc command in Linux with examples bc command is used for command line calculator. It is similar to basic calculator by using which we can do basic mathematical calculations. Arithmetic operations are the most basic in any kind of programming language. Linux or Unix operating system provides the bc command and expr command for doing
11 min read
bg command in Linux with Examples In Linux, the bg command is a useful tool that allows you to manage and move processes between the foreground and background. It's especially helpful when you want to multitask in the terminal by placing a process in the background, enabling you to continue using the terminal for other commands whil
3 min read