You will often have a list of items in your script that will each need to have a particular action performed on them. The for loop construct can help with this. Its general form is as follows:

for variable in list-of-items...

Here, variable gets set to each value in the list-of-items, and each command is run for each variable. Here is a simple example:

for FILE in acme1 report7 /tmp/junk5
   lp $FILE && rm $FILE

The previous code will print a hard copy of the three files in the list of items: acme1, report7, and /tmp/junk5. If the lp command does not generate an error, the code will then delete the file.

You can use backquotes to generate a much larger list of items to print and then remove. In the following example, ...

Get Practical UNIX now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.