<tt>for <em>var</em> in <em>list</em></tt>



next up previous contents
Next: while condition Up: Control Structures Previous: Control Structures

for var in list

The last example could easily be rewritten as

    #!/bin/sh
    for i in $*
    do
        echo The parameter is $i.
    done
    exit 0

In the above program $* could have been replaced by any list of names. The program would then be changed to:

    #!/bin/sh
    for i in Claude Paul Wayne Roger Tom
    do
        echo The name is $i.
    done
    exit 0

Within the shell, parameters are read as $1 for the first parameter, $2 for the second parameter, $3 for the third parameter, and so on. $* is the entire list of parameters.

If the ``in list" is omitted, the list taken is the list of parameters passed to the shell on the command line.



Super-User
Fri Feb 17 15:55:40 EST 1995