title: Assigning a value having semicolon (';') to a variable in bash
body: <p>I'm trying to escape ('\') a semicolon (';') in a string on unix shell (bash) with sed. It works when I do it directly without assigning the value to a variable. That is,</p>
  
  <pre><code>$ echo "hello;" | sed 's/\([^\\]\);/\1\\;/g'
  hello\;
  $
  </code></pre>
  
  <p>However, it doesn't appear to work when the above command is assigned to a variable:</p>
  
  <pre><code>$ result=`echo "hello;" | sed 's/\([^\\]\);/\1\\;/g'`
  $ 
  $ echo $result
  hello; 
  $
  </code></pre>
  
  <p>Any idea why?</p>
  
  <p>I tried by using the value enclosed with and without quotes but that didn't help. Any clue greatly appreciated.</p>
  
  <p>btw, I first thought the semicolon at the end of the string was somehow acting as a terminator and hence the shell didn't continue executing the <code>sed</code> (if that made any sense). However, that doesn't appear to be an issue. I tried by using the semicolon not at the end of the string (somewhere in between). I still see the same result as before. That is, </p>
  
  <pre><code>$ echo "hel;lo" | sed 's/\([^\\]\);/\1\\;/g'
  hel\;lo
  $
  $ result=`echo "hel;lo" | sed 's/\([^\\]\);/\1\\;/g'`
  $
  $ echo $result
  hel;lo
  $
  </code></pre>