The while Loop

The while loop enables you to execute a set of commands repeatedly until some condition occurs. It is normally used when you need to manipulate the value of a variable repeatedly. The basic syntax of the while loop is

while cmd
do
    list
done

Here, cmd is a single command, whereas list is a list of one or more commands. Although command can be any valid UNIX command, it is usually a test expression of the type covered in Chapter 11, “Flow Control.” The list called list is commonly referred to as the body of the while loop. The do and done keywords are not considered part of the body of the loop because the shell uses them only for determining where the while loop begins and ends.

The execution of a while loop proceeds according ...

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.