title: Escape dollar sign in regexp for sed
body: <p><em>I will introduce what my question is about before actually asking - feel free to skip this section!</em></p>
  
  <p><strong>Some background info about my setup</strong></p>
  
  <p>To update files manually in a software system, I am creating a bash script to remove all files that are not present in the new version, using diff:</p>
  
  <pre><code>for i in $(diff -r old new 2&gt;/dev/null | grep "Only in old" | cut -d "/" -f 3- | sed "s/: /\//g"); do echo "rm -f $i" &gt;&gt; REMOVEOLDFILES.sh; done
  </code></pre>
  
  <p>This works fine. However, apparently my files often have a dollar sign ($) in the filename, this is due to some permutations of the GWT framework. Here is one example line from the above created bash script:</p>
  
  <pre><code>rm -f var/lib/tomcat7/webapps/ROOT/WEB-INF/classes/ExampleFile$3$1$1$1$2$1$1.class
  </code></pre>
  
  <p>Executing this script would not remove the wanted files, because bash reads these as argument variables. Hence I have to escape the dollar signs with "\$".</p>
  
  <p><strong>My actual question</strong></p>
  
  <p>I now want to add a sed-Command in the aforementioned pipeline, replacing this dollar sign. As a matter of fact, sed also reads the dollar sign as special character for regular expressions, so obviously I have to escape it as well.
  But somehow this doesn't work and I could not find an explanation after googling a lot. </p>
  
  <p>Here are some variations I have tried:</p>
  
  <pre><code>echo "Bla$bla" | sed "s/\$/2/g"        # Output: Bla2
  echo "Bla$bla" | sed 's/$$/2/g'        # Output: Bla
  echo "Bla$bla" | sed 's/\\$/2/g'       # Output: Bla
  echo "Bla$bla" | sed 's/@"\$"/2/g'     # Output: Bla
  echo "Bla$bla" | sed 's/\\\$/2/g'      # Output: Bla
  </code></pre>
  
  <p>The desired output in this example should be "Bla2bla".
  What am I missing?
  I am using GNU sed 4.2.2</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I just realized, that the above example is wrong to begin with - the echo command already interprets the $ as a variable and the following sed doesn't get it anyway... Here a proper example:</p>
  
  <ol>
  <li>Create a textfile <code>test</code> with the content <code>bla$bla</code></li>
  <li><code>cat test</code> gives <code>bla$bla</code></li>
  <li><code>cat test | sed "s/$/2/g"</code> gives <code>bla$bla2</code></li>
  <li><code>cat test | sed "s/\$/2/g"</code> gives <code>bla$bla2</code></li>
  <li><code>cat test | sed "s/\\$/2/g"</code> gives <code>bla2bla</code></li>
  </ol>
  
  <p>Hence, the last version is the answer. Remember: <em>when testing, first make sure your test is correct, before you question the test object........</em></p>