title: shell - temp IFS as newline only. Why doesn't this work: IFS=$(echo -e '\n')
body: <p>I'm trying to use <code>for</code> in the shell to iterate over filenames with spaces. I read in a <a href="https://stackoverflow.com/questions/4128235/bash-shell-scripting-what-is-the-exact-meaning-of-ifs-n">stackoverflow question</a> that <code>IFS=$'\n'</code> could be used and that seems to work fine. I then read <a href="https://stackoverflow.com/questions/4128235/bash-shell-scripting-what-is-the-exact-meaning-of-ifs-n#comment9330981_4128305">in a comment to one of the answers</a> that to make it posix compatible for shells other than bash one could use <code>IFS=$(echo -e '\n')</code>.</p>
  
  <p>The former works but the latter doesn't. The comment is upvoted several times. I checked the output and the variable doesn't seem to contain the newline.</p>
  
  <pre><code>#!/bin/sh
  
  IFS=$(echo -e '\n')
  echo -n "$IFS" | od -t x1
  for file in `printf 'one\ntwo two\nthree'`; do
      echo "Found $file"
  done
  
  echo
  
  IFS=$'\n'
  echo -n "$IFS" | od -t x1
  for file in `printf 'one\ntwo two\nthree'`; do
      echo "Found $file"
  done
  </code></pre>
  
  <p><br>
  The output shows the field separator is missing for the first:  </p>
  
  <pre><code>0000000
  Found one
  two two
  three
  </code></pre>
  
  <p><br>
  But is proper on the second:  </p>
  
  <pre><code>0000000 0a
  0000001
  Found one
  Found two two
  Found three
  </code></pre>
  
  <p><br></p>
  
  <p>I found an answered question which may or may not be a duplicate of my question:<br>
  <a href="https://stackoverflow.com/questions/16831429/when-setting-ifs-to-split-on-newlines-why-is-it-necessary-to-include-a-backspac">linux - When setting IFS to split on newlines, why is it necessary to include a backspace? - Stack Overflow</a></p>
  
  <p>Is there any danger to doing what's discussed there, <code>IFS=$(echo -en '\n\b')</code>? Because that adds a <code>\b</code> to IFS (I checked). Can a <code>\b</code> occur in a file name? I don't want my code to split a file name by mistake.</p>
  
  <p>Also do you have any recommendations on how to properly handle restoration of IFS after the <code>for</code> loop? Should I store it in a temporary variable, or run IFS and the for statement in a subshell? (and how to do that?) Thanks</p>