title: bash script to replace all occurrences of placeholders in file
body: <p>I'm trying to write a bash script to replace all occurrences of a placeholder in a file with an environment variable of the same name. As an example, if I have a file like the following...</p>
  
  <pre><code>This is an {{VAR1}} {{VAR2}}.
  It should work across multiple lines in this {{VAR2}}.
  </code></pre>
  
  <p>... and I have the following environment variables set:</p>
  
  <pre><code>VAR1='example'
  VAR2='file'
  </code></pre>
  
  <p>after running the script on my file, I should get the output:</p>
  
  <pre><code>This is an example file.
  It should work across multiple lines in this file.
  </code></pre>
  
  <p>I'm sure there must be a solution using awk/sed, but so far the closest I've come can't handle if there's more than one variable on a line. Here's my attempt so far:</p>
  
  <pre><code>cat example.txt | grep -o '{{.*}}' &gt; temp
  while read placeholder; do
    varName=$(echo "$placeholder" | tr -d '{}')
    value="${!varName}"
    sed -i "s/$placeholder/$value/g" "$file"
  done &lt; temp
  rm -rf temp
  </code></pre>