break Command in Linux



The break command is a handy utility that is often used within loops to terminate or exit the loop prematurely based on certain conditions.

When you have your script running a loop (e.g., for, while, or until). The loop executes its body repeatedly based on the specified condition. When you include the break statement at some point within the loop to meet a specific (e.g., if [ $i -eq 3 ]; then break).

As soon as you include the break statement, it is executed and the loop stops running instantly. After the loop terminates, the program continues executing the next statement immediately following the loop. This means the script moves on to the next line of code outside the loop.

Whether it's a for, while, until, or select loop, the break command provides a convenient way to escape from a loop’s execution.

Loops are structures in programming that allow you to repeat a block of code multiple times. These are Loops are fundamental because they help automate repetitive tasks, making your code more efficient and easier to manage.

In Bash scripting, loops are particularly useful for tasks like processing files, running commands multiple times, or iterating over a list of items.

Table of Contents

Here is a comprehensive guide to the options available with the break command −

Syntax of break Command

The syntax of the break command takes the following form −

break [n]

[n] This is a parameter that specifies which loop to exit. If you have nested loops, n determines which level of the loop to break out of.

break Command Options

The following are available for the break command −

Options Description
[n]

nth enclosing loop: refers to the loop at the nth level of nesting. For example, if "n" is 1, it exits the current loop. If "n" is 2, it exits the loop that encloses the current loop, and so on.

"n" must be greater than or equal to 1. This ensures that you are referencing a valid loop level.

Return status: The command returns a status of zero if n is not greater than or equal to 1. If n is not valid (less than 1), it returns a non-zero status indicating an error.

Examples of break Command in Linux

In this section, we'll explore some examples of how to use the break command in different types of loops in Linux −

  • Using break in a for Loop
  • Using break in a while Loop
  • Using break in an until Loop
  • Using break in an Infinite Loop

Using break in a for Loop

To create and execute a bash script that uses a break command in a for loop, first create the script file using your preferred text editor −

nano break_loop.sh

Add the following code to the file −

#!/bin/bash
#breaksample
for i in {1..6}; do
   echo $i
   if [ $i -eq 4 ]; then
      break  # Exits the loop when i equals 4
   fi
done
echo "For loop terminated"

Save the changes to the file and make the script executable using the following syntax −

chmod +x break_loop.sh

Now run the script −

./break_loop.sh

This script will print numbers from 1 to 4 and then terminate the loop when i equals 4.

Using break in for Loop

Using break in a while Loop

To create and execute a bash script that uses a break statement in a while loop, first create a script file using your preferred text editor −

nano break_while_loop.sh

Add the following code in the file −

#!/bin/bash
#breaksample
count=1
while [ $count -le 10 ]; do
   echo $count
   if [ $count -eq 6 ]; then
      break  # Exits the loop when count equals 6
   fi
   ((count++))
done
echo "While loop terminated"

Save changes and make this script executable using the following command −

chmod +x break_while_loop.sh

Now, execute the script −

./break_while_loop.sh

This script will print numbers from 1 to 6 and then terminate the loop when count equals 6.

Using break in while Loop

Using break in an until Loop

To create and execute a bash script that uses a break statement in an until loop, start by creating a simple script file using your preferred text editor −

nano break_until_loop.sh

Add the following code to the file −

#!/bin/bash
#breaksample
number=1
until [ $number -gt 7 ]; do
   echo $number
   if [ $number -eq 5 ]; then
      break  # Exits the loop when number equals 5
   fi
   ((number++))
done
echo "Until loop terminated"

Save the changes and make the script executable using the following command −

chmod +x break_until_loop.sh

Now, execute the script −

./break_until_loop.sh

This script will print numbers from 1 to 5 and then terminate the loop when the number equals 5.

Using break in until Loop 1

To create and execute a bash script that uses a break statement in nested loops, you can start by creating a script file using a text editor −

nano break_nested_loops.sh

Add the following lines to the file −

#!/bin/bash
#breaksample
for i in {1..3}; do
   for k in {1..3}; do
      if [ $k -eq 2 ]; then
         break 2  # Exits both the inner and outer loops
      fi
      echo "$i $k"
   done
done
echo "Nested loops"

Save the changes and make the script executable with the following command −

chmod +x break_nested_loops.sh

Execute the script −

./break_nested_loops.sh

This script will print 1 1 and then terminate both the inner and outer loops when j equals 2.

Using break in until Loop 2

Using break in an Infinite Loop

To create and execute a bash script that uses a break statement in an infinite loop, you can create a script file using your preferred text editor −

nano break_infinite_loop.sh

Add the following code to the file −

#!/bin/bash
#breaksample
i=0
while :; do
   echo "Number: $i"
   ((i++))
   if [ $i -eq 5 ]; then
      break  # Exits the loop when i equals 5
   fi
done
echo "Infinite loop terminated"

Save the changes and make the script executable using the following command −

chmod +x break_infinite_loop.sh

Now, execute the script −

./break_infinite_loop.sh

This script will print numbers from 0 to 4 and then terminate the loop when i equals 5.

Using break in Infinite Loop

Conclusion

You can now use the break command to exit a loop prematurely. When the break statement is encountered, the loop stops executing, and the control moves to the next statement after the loop. Feel free to explore and use this command-line utility in bash scripting projects.

Advertisements