title: Str replace with Bash code chunk using three variables: search, replace and subject
body: <p>I have problems understanding the str replace function in Bash.
  I am coming from PHP where the function is named <a href="https://www.php.net/manual/en/function.str-replace.php" rel="nofollow noreferrer">str_replace</a>.</p>
  <p>Can anyone help me with the following example:</p>
  <pre><code>search=&quot;%body%&quot; # The value being searched for, otherwise known as the needle
  replace=&quot;black&quot; # The replacement value that replaces found search values
  subject=&quot;&lt;body text='%body%'&gt;&quot; # The string or array being searched and replaced on, otherwise known as the haystack
  
  new_string=`echo ${subject//$search/$replace}`
  echo &quot;$new_string&quot;
  </code></pre>
  <p>When I saved is at as a script &quot;replace.sh&quot; and run it then it prints:</p>
  <p>./replace.sh</p>
  <pre><code>&lt;body text='black'&gt;
  </code></pre>
  <p>When I put it into R Studio as a code chunk it doesnt print anything:</p>
  <pre><code>```{bash echo=FALSE, comment=&quot;&quot;, results=&quot;asis&quot;, message=FALSE, tidy=FALSE}
  search=&quot;%body%&quot; # The value being searched for, otherwise known as the needle
  replace=&quot;black&quot; # The replacement value that replaces found search values
  subject=&quot;&lt;body text='%body%'&gt;&quot; # The string or array being searched and replaced on, otherwise known as the haystack
  
  new_string=`echo ${subject//$search/$replace}`
  echo &quot;$new_string&quot;
  ```
  </code></pre>