title: Iterating over file (and directory) names with bash
body: <p>I was trying to write a bash script for counting the number of files and the number of directories of the local directory. This was my first try:</p>
  
  <pre><code>#!/bin/bash
  files=0
  dir=0
  for file in `ls`
  do
      if [ -d $file ]
      then
          dir=$(($dir+1))
      else
          files=$(($files+1))
      fi
  done 
  echo "files=$files, direcotries=$dir"
  </code></pre>
  
  <p>However, the <code>for</code> command is not iterating over the names of files and directories as I would expect. If the name of the file ou directory has spaces, this does not work well. When there are spaces in the names, the variable "file" assumes the value of each of the words in the file (or directory) name.</p>
  
  <p>Is there any way to do this?</p>