title: Sed finds backslash but doesn't replace it (escape character problem)?
body: <p>I want to replace <code>\emph{G. fortis}</code> with <code>\emph{G. fortis}\index{\emph{Geospiza fortis}}</code> to add terms to an index in a TeX document. I have a list of words in a file called <code>gfortis</code> that I will pass through the sed command in a <code>while read -r</code> command. </p>
  
  <pre><code>PREFTXt="\emph{G. fortis}" # text to search
  REPLACETXT="$PREFTXt\index{\emph{Geospiza fortis}}" # text to replace
  sed -e "s/${PREFTXt}/${REPLACETXT}/" path/chapt1.tex
  </code></pre>
  
  <p>The result is this: </p>
  
  <pre><code>\emph{G. fortis}index{emph{Geospiza fortis}}
  </code></pre>
  
  <p>But it should be: </p>
  
  <pre><code>\emph{G. fortis}\index{\emph{Geospiza fortis}}
  </code></pre>
  
  <p>The final command looks like that: </p>
  
  <pre><code>while read -r RP 
  do
  echo "Adding $RP to the index"
  PREFTXt="$RP"
  ADDTXt="\index{\emph{Geospiza fortis}}"
  REPLACETXT="$PREFTXt$ADDTXt"
  echo "Replaced $RP with $REPLACETXT"
  sed -e "s/${PREFTXt}/${REPLACETXT}/" path/chapt1.tex # should replace the text within this file. 
  done &lt; path/words_index/gfortis # input the words file to replace with a certain \index command 
  </code></pre>
  
  <p>The <code>cap1.txt</code> contains this:</p>
  
  <pre class="lang-tex prettyprint-override"><code>\chapter{Another chapter in the wall}
  
  NICE other\index{other} to be added to the index. 
  
  \emph{Geospiza fortis}
  All of the stuff that I put here shall be into the index. 
  
  \emph{Geospiza fortis}
  This index will be gigantic, but I won't be making multiple indexes. 
  
  \emph{G. fortis} 
  
  Other cool stuff here 
  
  \emph{G. fortis} 
  </code></pre>
  
  <p>I'm using bash in macOS Mojave</p>