break

shell builtinsLinux/Unix
The break command is one of the most frequently used commands in Linux/Unix-like operating systems. break The break command is a shell built-in that exits from a for, while, until, or select loop. It terminates the execution of the current loop and continues with the next command after the loop.

Quick Reference

Command Name:

break

Category:

shell builtins

Platform:

Linux/Unix

Basic Usage:

break 2

Common Use Cases

  • 1

    Loop control

    Exit from loops when certain conditions are met

  • 2

    Error handling

    Stop loop execution when an error occurs to prevent further processing

  • 3

    Nested loop management

    Exit from multiple levels of nested loops with a single command

  • 4

    Interactive menus

    Provide exit functionality in interactive shell script menus

Syntax

break [n]

Options

Option Description
n Optional integer argument that specifies how many nested loops to exit from. Default is 1, meaning it exits only the innermost loop.

Examples

How to Use These Examples

The examples below show common ways to use the break command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

#

Basic Examples:

# Simple break to exit a loop
for i in 1 2 3 4 5; do
  echo $i
  if [ $i -eq 3 ]; then
    break
  fi
done
echo "Loop exited"
# Using break in a while loop count=1 while true; do echo $count if [ $count -eq 5 ]; then break fi ((count++)) done
# Breaking from an until loop count=10 until [ $count -lt 1 ]; do echo $count if [ $count -eq 7 ]; then break fi ((count--)) done

Advanced Examples:

# Break with numeric argument to exit multiple nested loops
for i in 1 2 3; do
  echo "Outer loop: $i"
  for j in a b c; do
    echo "  Inner loop: $j"
    if [ $i -eq 2 ] && [ "$j" = "b" ]; then
      echo "  Breaking out of both loops"
      break 2
    fi
  done
done
echo "Both loops exited"
# Using break in a select menu echo "Select an option:" select option in "Option 1" "Option 2" "Option 3" "Exit"; do case $option in "Option 1") echo "You selected Option 1" ;; "Option 2") echo "You selected Option 2" ;; "Option 3") echo "You selected Option 3" ;; "Exit") echo "Exiting menu" break ;; *) echo "Invalid option" ;; esac done
# Using break in a case statement within a loop while read -p "Enter a command (quit to exit): " cmd; do case $cmd in "help") echo "Available commands: help, status, list, quit" ;; "status") echo "System status: OK" ;; "list") echo "Item 1, Item 2, Item 3" ;; "quit") echo "Exiting..." break ;; *) echo "Unknown command: $cmd" ;; esac done

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

Important Notes:

The break command is a shell built-in used to exit loop constructs in shell scripts.

Basic Usage:

  • Without any arguments, break exits only the innermost loop it's currently in.
  • It immediately terminates the loop without executing any remaining commands in the current iteration.
  • Control passes to the command following the terminated loop.

Breaking from Nested Loops:

  • When used with a numeric argument n, break n exits from n levels of nested loops.
  • For example, break 2 exits from the current loop and the loop that contains it.
  • This is particularly useful in complex scripts with deeply nested loops.

Compatible Loop Types:

  • for loops: Used for iterating over a list of items.
  • while loops: Execute as long as a condition is true.
  • until loops: Execute until a condition becomes true.
  • select loops: Used for creating simple menus in scripts.

Common Use Cases:

  • Early termination of loops when a specific condition is met.
  • Implementing error handling in loops to exit when an error occurs.
  • Providing exit mechanisms in interactive menu systems.
  • Optimizing performance by avoiding unnecessary iterations once a result is found.

Comparison with Continue:

  • While break exits the loop entirely, the continue command skips the rest of the current iteration and moves to the next iteration.
  • Use break when you want to completely exit the loop processing.
  • Use continue when you want to skip only the current iteration but continue with the loop.

Shell Compatibility:

  • The break command is available in all POSIX-compliant shells including bash, sh, ksh, and zsh.
  • The behavior of break n for nested loops may vary slightly between different shell implementations.

Tips & Tricks

1

Use the break command followed by a number to break out of a specific loop level

2

Use the break command followed by a pattern to break out of a loop when a condition is met

3

Use the break command without arguments to break out of the innermost loop

4

Use the continue command to skip the current iteration and continue with the next one

5

Use the exit command to exit the script entirely

Common Use Cases

Loop control

Exit from loops when certain conditions are met

Error handling

Stop loop execution when an error occurs to prevent further processing

Nested loop management

Exit from multiple levels of nested loops with a single command

Interactive menus

Provide exit functionality in interactive shell script menus

Conditional termination

Terminate repetitive processes early when a target result is achieved

Related Commands

These commands are frequently used alongside break or serve similar purposes:

Use Cases

1

Loop control

Exit from loops when certain conditions are met

2

Error handling

Stop loop execution when an error occurs to prevent further processing

3

Nested loop management

Exit from multiple levels of nested loops with a single command

4

Interactive menus

Provide exit functionality in interactive shell script menus

5

Conditional termination

Terminate repetitive processes early when a target result is achieved

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the break command works in different scenarios.

$ break
View All Commands