for with list Traditional version of for from the Bourne shell. For each element in given list until end of list, Assign an element from list to variable specified. Do Action body sequence of commands. Variable referenced in normal way with $. Referencing element variable not a requirement done for var in le1 le2 le3 etc. do action on $var other actions done
# This will list files in current directory as the input list for # the for statement # Then, for each file # it will do a long listing # followed by applying the file command # for fn in $( ls ) do # do long listing, toss any errors. ls -l $fn 2> /dev/null # do file on file. file $fn echo " " done | less |
#!/bin/bash forfun() { # quotes stripped from command line arguments. echo 'for a in $*' for a in $* do echo $a done echo " " # all cmd line arguments quoted as single string. echo 'for a in "$*"' for a in "$*" do echo $a done echo " " # quotes stripped from command line arguments. echo 'for a in $@' for a in $@ do echo $a done echo " " # cmd line argument quotes honored. echo 'for a in "$@"' for a in "$@" do echo $a done echo " " #abbreviated version of quoted $@ echo 'for a' for a do echo $a done } forfun one "and two" |
#!/bin/bash OIFS="$IFS" IFS=: cnt=1 for e in `cat /etc/passwd` do echo $e done IFS="$OIFS" |