Bash break and continue Statements

By 

Updated on

7 min read

Using break and continue to control Bash loops

When a Bash loop runs over a list of files, exit codes, or counter values, you will sometimes want to leave the loop early or skip a single bad entry without aborting the rest of the work. Bash provides two built-in statements for exactly that: break to leave the enclosing loop, and continue to jump straight to the next iteration.

This guide explains how both statements behave inside for, while, until , and select loops, including how the optional numeric argument lets you target an outer loop in nested constructs.

Bash break Statement

The break statement terminates the current loop and passes program control to the command that follows the terminated loop. It works inside any of the loop forms Bash supports: for, while, until, and select.

The syntax of the break statement takes the following form:

txt
break [n]

[n] is an optional argument and must be greater than or equal to 1. When [n] is provided, the nth enclosing loop is exited. break 1 is equivalent to break.

Breaking a While Loop

In the script below, the execution of the while loop will be interrupted once the counter reaches 2:

sh
i=0

while [[ $i -lt 5 ]]
do
  echo "Number: $i"
  ((i++))
  if [[ $i -eq 2 ]]; then
    break
  fi
done

echo 'All Done!'
output
Number: 0
Number: 1
All Done!

Breaking Nested Loops

When used inside nested for loops , break without an argument terminates the innermost enclosing loop. The outer loops are not terminated:

sh
for i in {1..3}; do
  for j in {1..3}; do
    if [[ $j -eq 2 ]]; then
      break
    fi
    echo "j: $j"
  done
  echo "i: $i"
done

echo 'All Done!'
output
j: 1
i: 1
j: 1
i: 2
j: 1
i: 3
All Done!

If you want to exit from the outer loop, use break 2. The argument 2 tells break to terminate the second enclosing loop:

sh
for i in {1..3}; do
  for j in {1..3}; do
    if [[ $j -eq 2 ]]; then
      break 2
    fi
    echo "j: $j"
  done
  echo "i: $i"
done

echo 'All Done!'
output
j: 1
All Done!

Bash continue Statement

The continue statement skips the remaining commands inside the body of the enclosing loop for the current iteration and passes program control to the next iteration of the loop.

The syntax of the continue statement is as follows:

txt
continue [n]

The [n] argument is optional and can be greater than or equal to 1. When [n] is given, the nth enclosing loop is resumed. continue 1 is equivalent to continue.

Skipping an Iteration in a While Loop

In the example below, once the current iterated item equals 2, the continue statement will cause execution to return to the beginning of the loop and continue with the next iteration:

sh
i=0

while [[ $i -lt 5 ]]; do
  ((i++))
  if [[ $i -eq 2 ]]; then
    continue
  fi
  echo "Number: $i"
done

echo 'All Done!'
output
Number: 1
Number: 3
Number: 4
Number: 5
All Done!

Skipping an Iteration in a For Loop

The same pattern works inside a for loop . The script below iterates over a fixed list of names and skips the entry bob:

sh
for user in alice bob carol dave; do
  if [[ "$user" == "bob" ]]; then
    continue
  fi
  echo "Greeting: $user"
done
output
Greeting: alice
Greeting: carol
Greeting: dave

When continue runs, Bash drops the rest of the loop body for that iteration and moves on to the next item in the list. The loop is not aborted, so dave is still processed at the end.

Continuing in Nested Loops

In nested loops, you can use continue 2 to skip the rest of the current iteration of the outer loop:

sh
for i in {1..3}; do
  for j in {1..3}; do
    if [[ $j -eq 2 ]]; then
      continue 2
    fi
    echo "i: $i, j: $j"
  done
done
output
i: 1, j: 1
i: 2, j: 1
i: 3, j: 1

Filtering Iterations with continue

The following script prints numbers from 1 through 50 that are divisible by 9. If a number is not divisible by 9, the continue statement skips the echo command and passes control to the next iteration of the loop:

sh
for i in {1..50}; do
  if [[ $(( $i % 9 )) -ne 0 ]]; then
    continue
  fi
  echo "Divisible by 9: $i"
done
output
Divisible by 9: 9
Divisible by 9: 18
Divisible by 9: 27
Divisible by 9: 36
Divisible by 9: 45

Practical Example

A common real-world use case is processing files in a directory and skipping certain entries. The following script processes all .log files in a directory but skips empty files:

sh
for file in /var/log/*.log; do
  if [[ ! -s "$file" ]]; then
    continue
  fi
  echo "Processing: $file ($(wc -l < "$file") lines)"
done

You can combine break and continue in the same loop. For example, to search for a specific string in files and stop after the first match:

sh
for file in /etc/*.conf; do
  if [[ ! -r "$file" ]]; then
    continue
  fi
  if grep -q "MaxSessions" "$file"; then
    echo "Found in: $file"
    break
  fi
done

Troubleshooting

break or continue outside a loop
You used break or continue outside of a loop, often by accident inside a function that no longer wraps a loop. Bash prints an error saying the statement is only meaningful in a for, while, or until loop. The script keeps running, but the statement does nothing. Move the call back inside a loop, or replace it with return if you only want to leave a function.

break inside a pipeline does not exit the outer script
A construct like cat file.txt | while read line; do ...; break; done runs the while loop in a subshell because of the pipe. The break exits that subshell, not the script that started the pipeline. Read from the file directly with redirection, for example while read line; do ...; break; done < file.txt. If the input comes from another command, use process substitution instead of a pipe.

break 0 or a negative argument
The optional numeric argument to break and continue must be greater than or equal to 1. A value of 0 or a negative number is rejected with an error and the loop is not affected. Pass 1 to target the innermost loop or 2 for the next enclosing loop.

Quick Reference

StatementEffect
breakExit the innermost loop
break nExit the nth enclosing loop
continueSkip to the next iteration of the innermost loop
continue nSkip to the next iteration of the nth enclosing loop

FAQ

Can I use break and continue outside a loop?
No. Using break or continue outside a loop will produce an error saying the statement is only meaningful in a for, while, or until loop. The script will continue running, but the statement has no effect.

What is the difference between break and exit?
break exits only the current loop and continues executing the rest of the script. exit terminates the entire script immediately.

Do break and continue work in select loops?
Yes. Both statements work in select loops the same way they work in for, while, and until loops.

Can I use break and continue in the same loop?
Yes. You can use both statements in the same loop. For example, continue to skip certain iterations and break to exit when a condition is met.

Conclusion

break and continue are the two statements that let you steer Bash loops without restructuring them, and the optional numeric argument is what makes them work cleanly inside nested loops. For a deeper look at the loop forms themselves, see the guides on Bash for loops and Bash while loops .

Tags

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page