18.5. for loop

The general format of the for loop is:

for variable_name in list do   command1   command . . . done 

The for loop processes all the information once for each value contained in list. To access each value in list use variable_name. The commands can be any valid shell command or statement. Variable_name can be any word you want.

The use of in list is optional; if you do not include it, the for loop uses the positional parameters from the command line.

The in list can contain substitution, strings and filenames. Let’s look at some examples.

18.5.1. Simple for loop

This loop simply echoes out the list, which is ‘1 2 3 4 5’. To access each variable the variable_name loop is used.

							$ pg for_i #!/bin/sh # for_i for loop in 12345 do ...

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.