title: Using bash -c with loops and standard output redirection
body: <p>Why does the standard output from the following commands differ?</p>
  
  <p>command1:</p>
  
  <pre><code>for ((i=0; i&lt;10; i=i+1)); do echo $i; done
  </code></pre>
  
  <p>command2:</p>
  
  <pre><code>bash -c "for ((i=0; i&lt;10; i=i+1)); do echo $i; done"
  </code></pre>
  
  <p>command1 prints the integers 1 to 10 on a separate line, as expected.  command2 prints 10 on each line.</p>
  
  <p>What do I need to do to get command2 to print the same thing as command1?</p>
  
  <p>The long story:</p>
  
  <p>I often need to run an executable on a large number of files and capture the output of the entire process.  I use a for loop similar to command1 in a separate file called my_script.sh.</p>
  
  <p>I can then get the output using:</p>
  
  <pre><code>bash my_script.sh &gt; results.txt
  </code></pre>
  
  <p>Since it's a bit of a pain to create a separate my_script.sh file for trivial for loops, I was hoping to achieve the same thing by using "bash -c", but ran into this problem.</p>
  
  <p>Thanks for any suggestions.  Looking forward to replies.<br>
  Misha</p>