title: Is there a better way to double escape (or escape twice) user input in BASH than calling printf twice?
body: <p>This script will read an old and a new value from the user and then use sed to find and replace them in a file.  For example if I entered T<em>T</em>z and B<em>B</em>z it would look for T\<em>T\</em>z in the file and replace with B\<em>B\</em>z.  It works but I've been trying to make this more concise.  </p>
  
  <p>I don't have any need for the intermediate variables $ESC_OLD_PW and $ESC_NEW_PW.  Is there a more <strong><em>sensible</em></strong> way to do this?  </p>
  
  <pre><code>    #!/bin/bash
      read -sp "Old:" OLD_PW &amp;&amp; echo
      read -sp "New:" NEW_PW &amp;&amp; echo
  
      # Add escape characters to what user entered
      printf -v ESC_OLD_PW "%q" "${OLD_PW}"
      printf -v ESC_NEW_PW "%q" "${NEW_PW}"
  
      # Escape again for the sed evaluation.
      printf -v ESC_ESC_OLD_PW "%q" "${ESC_OLD_PW}"
      printf -v ESC_ESC_NEW_PW "%q" "${ESC_NEW_PW}"
  
      sed -i -e s/"${ESC_ESC_OLD_PW}"/"${ESC_ESC_NEW_PW}"/g $1
  </code></pre>
  
  <p>I've tried the following:</p>
  
  <pre><code>    ~$ OLD_PW="T*T*z"
      ~$ printf "%q" $OLD_PW | xargs printf "%q"
      printf: %q: invalid conversion specification
      ~$
  </code></pre>
  
  <p>And I've tried many variations on piping things into printf...  Any suggestions?</p>