title: How can I use a regex variable for a find and replace command in bash?
body: <p>This is a follow-up to a previous <a href="https://stackoverflow.com/q/64053771/">question</a> I had asked.
  For that question, I received an excellent answer by @markp-fuso demonstrating the use of an array of search patters for <code>sed</code> to perform a particular substitution task.</p>
  <p>In this question, I would like to perform the same find and replace task of replacing all forms of <code>pow(var,2)</code> with <code>square(var)</code> but I would like to do this by using a regex variable.</p>
  <p>The sample input and output files are below:</p>
  <p><strong>InputFile.txt:</strong></p>
  <pre><code>pow(alpha,2) + pow(beta,2)
  (3*pow(betaR_red,2))
  2/pow(gammaBlue,3))
  -pow(epsilon_gamma,2)+5
  </code></pre>
  <p><strong>OutputFile.txt:</strong></p>
  <pre><code>square(alpha) + square(beta)
  (3*square(betaR_red))
  2/pow(gammaBlue,3))
  -square(epsilon_gamma)+5
  </code></pre>
  <p>After looking at <a href="https://stackoverflow.com/a/23756210">this answer</a> and some more tinkering on <a href="https://regex101.com/r/80Hp5Z/1/" rel="nofollow noreferrer">https://regex101.com/r/80Hp5Z/1/</a>, I was able to describe the text between <code>pow(</code> and <code>,2)</code> using <code>(?&lt;=pow\().*?(?=,2\))</code>.</p>
  <p>Using Bash, I am trying to form a two command solution where:</p>
  <ol>
  <li>First command sets <code>var</code> to <code>(?&lt;=pow\().*?(?=,2\))</code></li>
  <li>Second command performs <code>sed &quot;s/pow(${var},2)/square(${var})/g&quot; InputFile.txt &gt; OutputFile.txt</code></li>
  </ol>
  <p>It seems once I figure out how to set <code>var</code> successfully in Step 1, I can
  proceed with the command in Step 2. However, after reading this <a href="https://stackoverflow.com/questions/22229449/how-to-use-variables-in-linux-regex-pattern">question</a> and this <a href="https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash">question</a> I tried the following two commands but they did not perform any substitutions:</p>
  <pre><code>bash$ var=&quot;(?&lt;=pow\().*?(?=,2\))&quot;
  bash$ sed &quot;s/pow(${var},2)/square(${var})/g&quot; InputFile.txt &gt; OutputFile.txt
  </code></pre>
  <p>I would really appreciate some help forming a 2-command Bash solution as described above
  that makes use of a regex variable and transforms <code>InputFile.txt</code> into
  <code>OutputFile.txt</code>.</p>