18.8. Controlling loops using break and continue

Occasionally when processing you may need to break out or skip certain iterations based on some form of criteria. To help you achieve this the shell provides two commands:

  • break

  • continue.

18.8.1. break

The break command allows you to break out of loops. Breaks are usually used to quit a loop or case statement after some form of processing. If you are inside a nested loop, you can specify how many loops you should break out of: for instance if you are in a loop within a loop (that’s two loops down), then issue break 2 to come right out.

18.8.2. Breaking out of a case statement

Here’s an example. This script will loop forever until the user inputs a number above the number 5. To break out of this ...

Get Linux and Unix Shell Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.