Loop Control

So far you have looked at creating loops and working with loops to accomplish different tasks. Sometimes you will need to stop a loop or skip iterations of the loop. In this section you'll examine the break and continue commands that are used to control the execution of loops.

Infinite Loops and the break Command

Recall that the while loop terminated when a particular condition was met. This happened when the task of the while loop completed. If you make a mistake in specifying the termination condition of a while loop, it can continue forever. For example, say you forgot to specify the $ before the x in the test expression:

x=0
while [ x -lt 10 ]
do
    echo $x
     x=`expr $x + 1`
done

This loop would continue to display numbers forever. ...

Get Sams Teach Yourself Shell Programming in 24 Hours, Second Edition 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.