title: Echo valid certificate file from single line variable with bash
body: <p>I have a certificate stored in an environment variable on a single line, like this:</p>
  
  <pre><code>$EXAMPLE=-----BEGIN CERTIFICATE----- line1 line2 line3 etc.. -----END CERTIFICATE-----
  </code></pre>
  
  <p>How can I go from this environment var to a file, where the lines are all converted to newlines, except for the -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- parts?</p>
  
  <p>I know I can do this:</p>
  
  <pre><code>$ echo "$EXAMPLE" | tr ' ' '\n' &gt; ca.pem
  </code></pre>
  
  <p>It will replace all spaces to newlines, but this will result in:</p>
  
  <pre><code>-----BEGIN
  CERTIFICATE-----
  line1
  line2
  line3
  etc..
  -----END
  CERTIFICATE-----
  </code></pre>
  
  <p>Almost there... but the BEGIN and END lines with '-----' should not break. Anyone has a clean solution for this? Doesn't matter if it is sed, awk, grep, whatever :)</p>
  
  <p>Thanks!</p>