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



  • for - keyword.
  • var - variable name, one word variable name, standard rules. Do not use $ here. for assigns values from the list.
  • in - keyword preceding list of strings to process.
  • le1 le2 le3, etc. - a word list of values to assign to variable. List is the interesting part of this.
  • actual space delimited list of strings one two "and a tree" This is a 3 element list. Each time through the loop the next word is assigned. Quoted strings are a single element.
  • lists can be generated as the result of a command. Use graves or $() to run command to generate list.
    # 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
    
    Working with strings with embedded spaces, e.g "Hi there", when passed as arguments.
    #!/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"
    
    Using modified internal field separator, IFS, to change the separator use to parse a list.
    #!/bin/bash
    
    OIFS="$IFS"
    IFS=:
    cnt=1
    
    for e in `cat /etc/passwd`
    do
      echo $e
    done
    IFS="$OIFS"