<p>I have all my env vars in .env files.
They get automatically loaded when I open my shell-terminal.</p>
<p>I normally render shell environment variables into my target files with <code>envsubst</code>. similar to the example below.</p>
<h3>What I search is a solution where I can pass a <code>dotenv</code>-file as well my <code>template</code>-file to a script which outputs the rendered result.</h3>
<p>Something like this:</p>
<pre><code>aScript --input .env.production --template template-file.yml --output result.yml
</code></pre>
<p>I want to be able to parse different environment variables into my yaml. The output should be sealed via &quot;Sealed secrets&quot; and finally saved in the regarding kustomize folder</p>
<pre><code>envsub.sh .env.staging templates/secrets/backend-secrets.yml | kubeseal -o yaml &gt; kustomize/overlays/staging
</code></pre>
<p>I hope you get the idea.</p>
<hr />
<p>example</p>
<p><code>.env.production</code>-file:</p>
<p>FOO=bar
PASSWROD=abc</p>
<p>content of <code>template-file.yml</code></p>
<pre><code>stringData:
  foo: $FOO
  password: $PASSWORD
</code></pre>
<p>Then running this:</p>
<pre><code>envsubst &lt; template-file.yml &gt; file-with-vars.yml
</code></pre>
<p>the result is:</p>
<pre><code>stringData:
  foo: bar
  password: abc
</code></pre>
<p>My approach so far does not work because Dotenv also supports <strong>different environments</strong> like <code>.env</code>, <code>.env.production</code>, <code>.env.staging</code> asf..</p>

Jan
<p>What about:</p>
<pre><code>#!/bin/sh

# envsub - subsitute environment variables

env=$1
template=$2

sh -c &quot;
. \&quot;$env\&quot;

cat &lt;&lt;EOF
$(cat &quot;$template&quot;)
EOF&quot;
</code></pre>
<p>Usage:</p>
<pre><code>./envsub .env.production template-file.yaml &gt; result.yaml
</code></pre>
<ul>
<li>A here-doc with an unquoted delimiter (<code>EOF</code>) expands variables, whilst preserving quotes, backslashes, and other shell sequences.</li>
<li><code>sh -c</code> is used like <code>eval</code>, to expand the command substitution, then run that output through a here-doc.</li>
<li>Be aware that this extra level of indirection creates potential for code injection, if someone can modify the yaml file.</li>
</ul>
<p>For example, adding this:</p>
<pre><code>EOF
echo malicous commands
</code></pre>
<p>But it does get the result you want.</p>
