title: Bash sed: Replace all special characters (Invalid range end)
body: <p>Ultimate goal is to replace line containing a password (which can contain any character) in a file:</p>
  
  <pre><code>...
  passw=old#$#$@$#@%{}[]
  ...
  </code></pre>
  
  <p>I have string variables with replacements: 
  (dont mind the double quotes for now)</p>
  
  <p><code>$old="passw=old#$#$@$#@%{}[]"</code></p>
  
  <p><code>$new="passw=new#$#$@$#@%{}[]"</code></p>
  
  <p>If I use simple <code>sed -i "s#$old#$new#g" file.txt"</code> it obviously does't work properly as special characters break the syntax.</p>
  
  <p>To make it work I need to escape all special characters with <code>\</code>, ex.: <code>\[</code> </p>
  
  <p>I tried to use something like this:</p>
  
  <pre><code>esc=$("$old" | sed 's/[][`~!@#$%^&amp;*()-_=+{}\|;:",&lt;.&gt;/?'"'"']/\\&amp;/g'
  </code></pre>
  
  <p>However it gives me the following error:</p>
  
  <pre><code>sed: -e expression #1, char42: Invalid range end
  </code></pre>
  
  <p>Any help would be greatly appreciated!</p>