next up previous contents
Next: The break and continue Up: Control Previous: The while command   Contents

The for command

The above sh script can be rewritten to use for:-

#!/bin/bash
for var
do
  echo $var
done
exit 0

By default the variable ``var'' is assigned the value of each input argument in turn. This works even if there are null string arguments. However you can provide a list of values:-

#!/bin/bash
for var  in  abc \
             def \
             ghi
do
  echo $var
done
exit 0

which produces the output:-

abc
def
ghi



P.D. Gronbech (IT Staff) 2015-10-02