title: sed does not work as expected with variables in script
body: <p>I have a small script that does some <code>global</code> string replacements in various files. This should be done with <code>sed</code>so that only the affected lines are changed.</p>
  
  <p>Preparation:</p>
  
  <pre><code>#!/bin/sh
  find="Mücke Motorsport"
  replace="Mücke Motorsport Racing Team"
  file="/Users/meyer/Dropbox/Dev/App Framework iOS/dev/myfile.txt"
  </code></pre>
  
  <p>This is my problematic line in the script:</p>
  
  <pre><code>sed -i s/"$find"/"$replace"/g "$file"
  </code></pre>
  
  <p>Does not work (<code>sed: 1: "/Users/meyer/Dropb ...": invalid command code o</code>). Next try:</p>
  
  <pre><code>sed -i 's/'"$find"'/'"$replace"'/g' "$file"
  </code></pre>
  
  <p>Does not work (<code>sed: 1: "/Users/meyer/Dropb ...": invalid command code o</code>). Next try:</p>
  
  <pre><code>sed -i "s/$find/$replace/g" "$file"
  </code></pre>
  
  <p>Does not work (<code>sed: 1: "/Users/meyer/Dropb ...": invalid command code o</code>). Next try:</p>
  
  <pre><code>sed -i s/$find/$replace/g "$file"
  </code></pre>
  
  <p>Does not work (<code>sed: 1: "Motorsport/Mücke": invalid command code M</code>). Because of this error message I tried to escape my variables:</p>
  
  <pre><code>escaped_ find =$(printf '%s\n' "$find" | sed 's:[][\/.^$*]:\\&amp;:g')
  escaped_ replace =$(printf '%s\n' "$replace" | sed 's:[\/&amp;]:\\&amp;:g;$!s/$/\\/')
  </code></pre>
  
  <p>But usage of the replace variables does´t work ... I don´t know how to get this working :(</p>