title: How can I escape white space in a bash loop list?
body: <p>I have a bash shell script that loops through all child directories (but not files) of a certain directory.  The problem is that some of the directory names contain spaces.  </p>
  
  <p>Here are the contents of my test directory:</p>
  
  <pre><code>$ls -F test
  Baltimore/  Cherry Hill/  Edison/  New York City/  Philadelphia/  cities.txt
  </code></pre>
  
  <p>And the code that loops through the directories:</p>
  
  <pre><code>for f in `find test/* -type d`; do
    echo $f
  done
  </code></pre>
  
  <p>Here's the output:</p>
  
  <pre>
  test/Baltimore
  test/Cherry
  Hill
  test/Edison 
  test/New
  York
  City
  test/Philadelphia
  </pre>
  
  <p>Cherry Hill and New York City are treated as 2 or 3 separate entries.</p>
  
  <p>I tried quoting the filenames, like so:</p>
  
  <pre><code>for f in `find test/* -type d | sed -e 's/^/\"/' | sed -e 's/$/\"/'`; do
    echo $f
  done
  </code></pre>
  
  <p>but to no avail.</p>
  
  <p>There's got to be a simple way to do this.</p>
  
  <hr>
  
  <p>The answers below are great.  But to make this more complicated - I don't always want to use the directories listed in my test directory.  Sometimes I want to pass in the directory names as command-line parameters instead.</p>
  
  <p>I took Charles' suggestion of setting the IFS and came up with the following:</p>
  
  <pre><code>dirlist="${@}"
  (
    [[ -z "$dirlist" ]] &amp;&amp; dirlist=`find test -mindepth 1 -type d` &amp;&amp; IFS=$'\n'
    for d in $dirlist; do
      echo $d
    done
  )
  </code></pre>
  
  <p>and this works just fine unless there are spaces in the command line arguments (even if those arguments are quoted).  For example, calling the script like this: <code>test.sh "Cherry Hill" "New York City"</code> produces the following output:</p>
  
  <pre>
  Cherry
  Hill
  New
  York
  City
  </pre>