title: Linux Replace With Variable Containing Double Quotes
body: <p>I have read the following:</p>
  
  <ul>
  <li><a href="https://askubuntu.com/questions/76808/how-do-i-use-variables-in-a-sed-command/76842">How Do I Use Variables In A Sed Command</a></li>
  <li><a href="https://unix.stackexchange.com/questions/69112/how-can-i-use-variables-when-doing-a-sed">How can I use variables when doing a sed?</a></li>
  <li><a href="https://stackoverflow.com/questions/31707923/sed-replace-variable-in-double-quotes">Sed replace variable in double quotes</a></li>
  </ul>
  
  <p>I have learned that I can use <code>sed "s/STRING/$var1/g"</code> to replace a string with the contents of a variable. However, I'm having a hard time finding out how to replace with a variable that contains double quotes, brackets and exclamation marks.</p>
  
  <p>Then, hoping to escape the quotes, I tried piping my result though <code>sed 's/\"/\\\"/g'</code> which gave me another error <code>sed: -e expression #1, char 7: unknown command: E'</code>. I was hoping to escape the problematic characters and then do the variable replacement: <code>sed "s/STRING/$var1/g"</code>. But I couldn't get that far either.</p>
  
  <p>I figured you guys might know a better way to replace a string with a variable that contains quotes.</p>
  
  <p>File1.txt:</p>
  
  <pre><code>Example test here
  &lt;tag&gt;"Hello! [world]" This line sucks!&lt;/tag&gt;
  End example file
  </code></pre>
  
  <p>Variable: </p>
  
  <pre><code>var1=$(cat file1.txt)
  </code></pre>
  
  <p>Example:</p>
  
  <pre><code>echo "STRING" | sed "s/STRING/$var1/g"
  </code></pre>
  
  <p>Desired output:</p>
  
  <pre><code>Example test here
  &lt;tag&gt;"Hello! [world]" This line sucks!&lt;/tag&gt;
  End example file
  </code></pre>