These classnotes are depreciated. As of 2005, I no longer teach the classes. Notes will remain online for legacy purposes

UNIX01/Iteration

Classnotes | UNIX01 | RecentChanges | Preferences

Showing revision 1

for

Within a shell file the for structure without a specified list of values takes as its list of values the arguments specified on the command line when the shell was invoked. The syntax for "for" is as follows:
 for variable
    do
      commands
    done

The variable used in the for command is set automatically to each argument value in sequence. For example, if we had a file called "echofname" that contained this:

  for fname
  do
     echo "$fname"
  done

and we called it thusly:

 $ echofname bob.txt frank.txt george.dat

we would get

 bob.txt
 frank.txt
 george.dat

for - in

If in the for structure we specify a list of values, we can explicitly set the contents of the variable in the loop. For example, the following code

 for grocery in milk cookies apples cheese
   do
     echo "Pick up $grocery"
   done

would provide us with

 Pick up milk
 Pick up cookies
 Pick up apples
 Pick up cheese

while



Classnotes | UNIX01 | RecentChanges | Preferences
This page is read-only | View other revisions | View current revision
Edited July 26, 2003 4:16 am (diff)
Search:
(C) Copyright 2003 Samuel Hart
Creative Commons License
This work is licensed under a Creative Commons License.