title: Sed replace with shell variables
body: <p>I am trying to implement sed replace with shell variables. As far for now I have working sed replace with <code>'</code></p>
  
  <pre><code>sed -i -r 's#(export\ PATH=")(.*)#\1/home/USER/bin:~/.local/bin:\2#' ~/.zshrc
  </code></pre>
  
  <p>But unfortunately <code>'</code> does not expand shell variables just as stated in this answer <a href="https://stackoverflow.com/a/5156322/675100">https://stackoverflow.com/a/5156322/675100</a></p>
  
  <p>While trying to use <code>"</code> I keep getting problems with parentheses etc. I would like to make this generic to always use <code>whoami</code> instead of <code>USER</code></p>
  
  <p>The first attempt would be </p>
  
  <pre><code>sed -i -r "s#\(export\ PATH=\"\)(.*)#\1/home/`whoami`/bin:~/.local/bin:\2#" ~/.zshrc
  sed: -e expression #1, char 60: invalid reference \2 on `s' command's RHS
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>I have tried </p>
  
  <pre><code>sed -i -r "s#\(export\ PATH=\"\)\(.*\)#\1/home/$(whoami)/bin:~/.local/bin:\2#" ~/.zshrc
  </code></pre>
  
  <p>to escape the second group but I get the similar error : </p>
  
  <pre><code>sed: -e expression #1, char 63: invalid reference \2 on `s' command's RHS
  </code></pre>