title: Why isn't sed's output stored with its multiple backslashes in bash?
body: <p>I am trying to store the output of sed into a variable, however the output differs from what I expect.</p>
  
  <p>My test is the following one</p>
  
  <pre><code>$ foo="this is (foo)"
  $ x="$(sed 's/(/\\\\(/g' &lt;&lt;&lt; $foo)"
  </code></pre>
  
  <p>The result expected is :</p>
  
  <pre><code>$ echo $x
  this is \\(foo)
  </code></pre>
  
  <p>The result I get is :</p>
  
  <pre><code>$ echo $x
  this is \(foo)
  </code></pre>
  
  <p>However, when I do not assign the output to a variable, then result is the expected one :</p>
  
  <pre><code>$ sed 's/(/\\\\(/g' &lt;&lt;&lt; $foo
  this is \\(foo)
  </code></pre>
  
  <p><strong>Why is the storage of my output failing?</strong></p>
  
  <p>NOTE :</p>
  
  <p>I also tried the following command lines which all ended to the same result :</p>
  
  <pre><code>$ x=`sed 's/(/\\\\(/g' &lt;&lt;&lt; $foo`
  
  $ x=$(sed 's/(/\\\\(/g' &lt;&lt;&lt; $foo)
  
  $ x=`echo $foo | sed 's/(/\\\\(/g'`
  
  $ x=$(echo $foo | sed 's/(/\\\\(/g')
  </code></pre>