Bash Break and Continue

By 

Updated on

5 min read

Bash Break and Continue

Loops allow you to run one or more commands multiple times until a certain condition is met. However, sometimes you may need to alter the flow of the loop and end the loop or skip the current iteration.

In Bash, break and continue statements allow you to control loop execution.

Bash break Statement

The break statement terminates the current loop and passes program control to the command that follows the terminated loop. It is used to exit from a for, while, until , or select loop.

The syntax of the break statement takes the following form:

sh
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:

sh
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 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!

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 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

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 the error break: 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

The break statement is used to exit the current loop, and the continue statement is used to skip the current iteration and move to the next one. Both statements accept an optional numeric argument to control nested loops.

If you have any questions, feel free to leave a comment below.

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