title: Is there an "escape converter" for file and directory names available?
body: <p>The day came when I had to write a BASH script that walks arbitrary directory trees and looks at arbitrary files and attempts to determine something regarding a comparison among them. I thought it would be a simple couple-of-hours <em>tops!</em> process - Not So!</p>
  
  <p>My hangup is that sometimes some idiot -ahem!- excuse me, <em>lovely user</em> chooses to put spaces in directory and file names. This causes my script to fail.</p>
  
  <p><em>The perfect solution</em>, aside from threatening the guillotine for those who insist on using spaces in such places (not to mention the guys who put this in operating systems' code!), might be a routine that "escapes" the file and directory names for us, kind of like how cygwin has routines to convert from unix to dos filename formats. Is there anything like this in a standard Unix / Linux distribution?</p>
  
  <p>Note that the simple <code>for file in *</code> construct doesn't work so well when one is trying to compare directory trees as it <strong>ONLY</strong> works on "the current directory" - and, in this case as in many others, constantly CDing to various directory locations brings with it its own problems. So, in doing my homework, I found this question <a href="https://stackoverflow.com/questions/1563856/handle-special-characters-in-bash-for-in-loop">Handle special characters in bash for...in loop</a> and the proposed solution there hangs up on spaces in directory names, but can simply be overcome like this:</p>
  
  <pre><code>dir="dirname with spaces"
  ls -1 "$dir" | while read x; do
     echo $x
  done
  </code></pre>
  
  <p><strong>PLEASE NOTE:</strong> The above code isn't particularly wonderful because the variables used inside the while loop are INACCESSIBLE outside that while loop. This is because there's an implied subshell created when the ls command's output is piped. <strong>This is a key motivating factor to my query!</strong></p>
  
  <p>...OK, the code above helps for many situations but "escaping" the characters would be pretty powerful too. For example, dir above might contain:</p>
  
  <pre><code>dir\ with\ spaces
  </code></pre>
  
  <p>Does this already exist and I've just been overlooking it?</p>
  
  <p>If not, does anyone have an easy proposal to write one - maybe with sed or lex? (I'm far from competent with either.)</p>