title: "For" loop in bash script only run once
body: <p>The script goal is simple.</p>
  
  <p>I have many directory which contains some captured traffic files.
  I want to run a command for each directory. So I came up with a script. But I don't know why the script is run only with the first match.</p>
  
  <pre><code>#!/bin/bash
  # Collect throughput from a group of directory containing capture files
  # Group of directory can be specify by pattern
  # Usage: ./collectThroughputList [regex]
  #       [regex] is the name pattern of the group of directory
  
  for DIR in $( ls -d $1 ); do
     if test -d "$DIR"; then
         echo Collecting throughputs from directory: "$DIR"
         ( sh collectThroughput.sh $DIR &gt; $DIR.txt )
     fi
  done
     echo Done\!
  </code></pre>
  
  <p>I try it with:</p>
  
  <pre><code>for DIR in $1; do
  </code></pre>
  
  <p>or</p>
  
  <pre><code>for DIR in `ls -d $1`; do
  </code></pre>
  
  <p>or</p>
  
  <pre><code>for DIR in $( ls -d "$1" ); do
  </code></pre>
  
  <p>or</p>
  
  <pre><code>for DIR in $( ls -d $1 ); do
  </code></pre>
  
  <p>But the result is the same. The for loop runs only one time.</p>
  
  <p>Finally I found this one and did some tricks for it to work. However, I would like to know why my first script doesn't work.</p>
  
  <pre><code>find *Delay50ms* -type d -exec bash -c "cd '{}' &amp;&amp; echo enter '{}' &amp;&amp; ../collectThroughput.sh ../'{}' &gt; ../'{}'.txt" \;
  </code></pre>
  
  <p>"*Delay*" is the directory pattern name that I want to run the command with.
  Thanks for pointing out the issues.</p>