title: What do I need to quote in sed command lines?
body: <p>There are many questions on this site on how to escape various elements for sed, but I'm looking for a more general answer. I understand that I might want to escape some characters to avoid shell expansion:</p>
  
  <blockquote>
    <p><a href="http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_03.html" rel="nofollow">Bash</a>:</p>
    
    <ul>
    <li><strong>Single quoted [strings]</strong> ('') are used to preserve the literal value of each character enclosed within the quotes. [However,] a <strong><s>single quote</s></strong> may not occur between single quotes, even when preceded by a backslash.</li>
    <li>The backslash retains its meaning [in <strong>double quoted strings</strong>] only when followed by <strong>dollar</strong>, <strong>backtick</strong>, <strong>double quote</strong>, <strong>backslash</strong> or <strong>newline</strong>. Within double quotes, the backslashes are removed from the input stream when followed by one of these characters. Backslashes preceding characters that don't have a special meaning are left unmodified for processing by the shell interpreter.</li>
    </ul>
    
    <p><a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?sh" rel="nofollow">sh</a>: (I hope you don't have history expansion)</p>
    
    <ul>
    <li>Single quoted string behaviour: same as bash</li>
    <li>Enclosing characters in double quotes preserves the  literal  value  of
       all  characters  within the quotes, with the exception of <strong>dollar</strong>, <strong>single quote</strong>, <strong>backslash</strong>, and,
       when history expansion is enabled, <strong>exclamation mark</strong>.
    <ul>
    <li>The characters <code>dollar</code> and <strong>single quote</strong> retain  their  special meaning within double quotes.</li>
    <li>The backslash retains its special meaning only when followed by one of the following  characters: <code>$</code>, <code>'</code>,  <code>"</code>, <code>\</code>, or newline.  A <strong>double quote</strong> may be quoted within double
    quotes by preceding it with a backslash.</li>
    <li>If enabled, history expansion  will  be    performed  unless an <strong>exclamation mark</strong>   appearing in double quotes is escaped using a backslash.  The backslash preceding the !  is <em>not</em> removed.</li>
    </ul></li>
    </ul>
  </blockquote>
  
  <p>...but none of that explains why this stops working as soon as you remove any escaping:</p>
  
  <pre><code>sed -e "s#\(\w\+\) #\1\/#g" #find a sequence of characters in a line
  #    why? ↑   ↑ ↑     ↑     #replace the following space with a slash.
  </code></pre>
  
  <p>None of <code>(</code>, <code>)</code>, <code>/</code> or <code>+</code> (or <code>[</code>, or <code>]</code>...) seem to have any special meaning that requires them to be escaped in order to work. Hell, even calling the command directly through Python makes sed not work properly, although the manpage doesn't seem to spell out anything about this (not when I search for backslash, anyway.)</p>
  
  <pre><code>$ lvdisplay -C --noheadings -o vg_name,name &gt; test
  $ python
  &gt;&gt;&gt; import os
  &gt;&gt;&gt; #Python requires backslash escaping of \1, even in triple quotes
  &gt;&gt;&gt; #lest \1 is read to mean "byte with value 0x01".
  &gt;&gt;&gt; output = os.execl("/bin/sed", "-e", "s#(\w+) #\\1/#g", "test")
  (Output remains unchanged)
  $ python
  &gt;&gt;&gt; import os
  &gt;&gt;&gt; output = os.execl("/bin/sed", "-e", "s#\(\w\+\) #\\1\/#g", "test")
  (Correct output)
  $ WHAT THE HELL
  Have you tried using jQuery? It's perfect and it does all the things.
  </code></pre>