title: How do I substitute special characters with escaped version in a Bash script
body: <p>I swear I have looked and looked and looked. If I've missed the answer to this somewhere, I apologize; it means my search-fu failed me, not that I haven't tried.</p>
  
  <p>I'm relatively new to bash scripts (:sigh: newbie), but I'm trying to write one that takes some input from the user and uses it in another command. My problem is that the other command needs to have any special characters in the user input escaped.</p>
  
  <p>The basic form of the code is as follows:</p>
  
  <pre><code>#!/bin/bash
  
  echo "Enter info:"
  read _INFO
  
  othercommand info=$_INFO 
  </code></pre>
  
  <p>Between the <code>read</code> and <code>othercommand</code> lines, I need to massage <code>_INFO</code> so that any special characters - spaces, single and double quotes ... pretty much any non-alphanumeric character, really - are escaped. For instance, an input of <code>this pipe: | is my info!</code> should become <code>this\ pipe\:\ \|\ is\ my\ info\!</code></p>
  
  <p>Note: I cannot put quotation marks - single or double - into the <code>othercommand</code> line, as it causes the command to fail. The info string needs to go as is, except escaped.</p>
  
  <p>I've tried using the string substitution macro in various ways (here are some examples that only check for spaces):</p>
  
  <pre><code>_INFO = ${$_INFO// /\ }
  _INFO = ${$_INFO// /\\ }
  _INFO = ${$_INFO//" "/"\ "}
  _INFO = ${$_INFO//" "/"\\ "}
  _INFO = ${$_INFO//' '/'\ '}
  _INFO = ${$_INFO//' '/'\\ '}
  </code></pre>
  
  <p>but I keep getting "bad substitution" errors.</p>
  
  <p>So, not only is this too specific (only handles spaces) but it doesn't work anyway. How do I do this?</p>
  
  <p>If I have to use <code>sed</code> or <code>awk</code> or something else not native to bash script-handling, I'd prefer to be given the exact line I'd need to use, as I'm entirely unfamiliar with them. (:sigh: newbie)</p>