O'Reilly logo

Linux and Unix Shell Programming by David Tansley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required