Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

What continue and break Do

  • continue: stops the current pass of the loop and starts the next one.
  • break: leaves the loop; the script goes on after done.
  • continue N and break N: act on the Nth enclosing loop, for nested loops.

continue in a for Loop

#!/bin/bash
for user in root deploy backup; do
  if [ "$user" = "root" ]; then
    continue
  fi
  echo "Checking $user"
  id "$user"
done

For root, continue jumps straight to the next name, so echo and id run only for deploy and backup. A short form of the same test is [ "$user" = "root" ] && continue.

continue in a while Loop

A common use is skipping blank lines and comments while reading a file:

#!/bin/bash
while IFS= read -r line; do
  [[ -z "$line" || "$line" == "#"* ]] && continue
  echo "Host: $line"
done < hosts.txt

In a counting loop, change the counter before any continue. If the increment comes after it, a skipped pass never reaches the increment and the loop never ends:

#!/bin/bash
i=0
while [ "$i" -lt 10 ]; do
  i=$((i + 1))
  if [ $((i % 2)) -eq 0 ]; then
    continue
  fi
  echo "$i"
done

This prints the odd numbers from 1 to 9.

break: Leave a Loop Early

This script waits up to a minute for an application to answer its health check, then stops trying:

#!/bin/bash
for attempt in {1..30}; do
  if curl -fsS http://127.0.0.1:8080/health > /dev/null; then
    echo "Up after $attempt tries"
    break
  fi
  sleep 2
done

Without break, the loop would keep checking after the first success.

Nested Loops: continue 2 and break 2

#!/bin/bash
for site in /var/www/*/; do
  for file in index.html robots.txt; do
    if [ ! -f "${site}${file}" ]; then
      echo "${site}: missing $file"
      continue 2
    fi
  done
  echo "${site}: complete"
done

continue 2 leaves the inner loop and starts the next pass of the outer one, so a site with a missing file is reported once and skipped. break 2 would leave both loops. Without a number, both commands act on the innermost loop. If the number is larger than the depth of the loops, break leaves all of them.

Example: Skip Files You Cannot Process

#!/bin/bash
shopt -s nullglob
for file in /var/log/myapp/*.log; do
  [ -s "$file" ] || continue
  if [ ! -r "$file" ]; then
    echo "Cannot read $file" >&2
    continue
  fi
  gzip -k "$file"
done

nullglob makes the loop run zero times when no file matches, instead of once with the literal pattern. -s skips empty files, and the readability test skips files your user cannot open. gzip -k keeps the original next to the compressed copy.

continue, break, and set -e

With set -e the script exits when a command fails, unless the failure is tested by if, while, until, &&, or ||. That makes command || continue the way to skip one item without ending the whole script:

#!/bin/bash
set -e
for dir in /srv/app1 /srv/app2; do
  git -C "$dir" pull --ff-only || { echo "Pull failed in $dir" >&2; continue; }
  echo "Updated $dir"
done

One trap with set -e: ((count++)) returns a failure status when count was 0, and the script exits. Write count=$((count + 1)) instead. Git commands are explained in basic Git commands with examples.

Common Mistakes

  • Increment after continue: a while loop whose counter changes only after a continue never ends.
  • Loops fed by a pipe: in command | while read -r line, the loop runs in a subshell. break and continue work, but variables set inside are gone after done; read from a file with < instead.
  • Running a Bash script with sh: on Debian and Ubuntu that is dash, which does not know {1..30}; see types of shells in Linux.

Choosing between several values inside a loop is the job of the Bash case statement. Run long loops inside tmux so a dropped connection does not stop them; see the tmux cheat sheet.

Was this answer helpful?
Loading...