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

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