title: Preserving leading white space while reading>>writing a file line by line in bash
body: <p>I am trying to loop through a directory of text files and combine them into one document. This works great, but the text files contain code snippets, and all of my formatting is getting collapsed to the left. All leading whitespace on a line is stripped.</p>
  
  <pre><code>#!/bin/sh
  OUTPUT="../best_practices.textile"
  FILES="../best-practices/*.textile"
  for f in "$FILES"
  do
    echo "Processing $f file..."
    echo ""&gt;$OUTPUT
  
    cat $f | while read line; do 
        echo "$line"&gt;&gt;$OUTPUT
    done
    echo &gt;&gt;$OUTPUT
    echo &gt;&gt;$OUTPUT
  done
  </code></pre>
  
  <p>I am admittedly a bash noob, but after searching high and low I couldn't find a proper solution. Apparently BASH hates the leading white space in general.</p>