control-flow

break / continue

Loop-control builtins for for/while/until/select loops. 'break' exits the enclosing loop; 'continue' skips to the next iteration; both accept an integer N to affect N nested loops at once.

break [N]   |   continue [N]

Common flags / operators

Flag / Operator Purpose
break Exit the current loop entirely
continue Skip rest of body and start next iteration
N Apply to the Nth enclosing loop (default 1)
exit status Both always return 0

Examples

for i in 1 2 3; do (( i == 2 )) && break; echo "$i"; done

Prints only 1 — loop exits when i reaches 2.

for f in *; do [[ -d $f ]] && continue; process "$f"; done

Skip directories, process only files.

for a in x y; do for b in 1 2; do break 2; done; done

break 2 exits both nested loops at once.

for i in {1..5}; do (( i % 2 )) && continue; echo "$i"; done

Prints only even numbers 2 and 4.

Gotcha

'break' inside a function does NOT return from the function — it only breaks the loop. In an until loop, continue re-tests the condition — make sure loop state advances or you'll spin forever.

Related

← All Bash builtins · Linux commands