10.3. Loop Control

Commands Affecting Loop Behavior

break, continue

The break and continue loop control commands [1] correspond exactly to their counterparts in other programming languages. The break command terminates the loop (breaks out of it), while continue causes a jump to the next iteration of the loop, skipping all the remaining commands in that particular loop cycle.

The break command may optionally take a parameter. A plain break terminates only the innermost loop in which it is embedded, but a break N breaks out of N levels of loop.

The continue command, similar to break, optionally takes a parameter. A plain continue cuts short the current iteration within its loop and begins the next. A continue N terminates all remaining iterations at its loop level and continues with the next iteration at the loop N levels above.

Caution

The continue N construct is difficult to understand and tricky to use in any meaningful context. It is probably best avoided.

Notes

[1]

These are shell builtins, whereas other loop commands, such as while and case, are keywords.