<p>How do I pass a environment variable into a kubectl exec command, which is calling a script?</p>
<pre><code>kubectl exec client -n namespace -- /mnt/script.sh
</code></pre>
<p>In my <code>script.sh</code>, I need the value of the passed variable.</p>
<p>I tried:</p>
<pre><code>kubectl exec client -n namespace -- PASSWORD=pswd /mnt/script.sh
</code></pre>
<p>which errors with:</p>
<pre><code>OCI runtime exec failed: exec failed: unable to start container process: exec: &quot;PASSWORD=pswd&quot;: executable file not found in $PATH: unknown
</code></pre>

user3142695
<p>You can use <a href="https://linux.die.net/man/1/env" rel="nofollow noreferrer"><strong>env</strong>(1)</a></p>
<pre class="lang-bash prettyprint-override"><code>kubectl exec client -n namespace -- \
  env PASSWORD=pswd /mnt/script.sh
</code></pre>
<p>or explicitly wrap the command in an <strong>sh</strong>(1) invocation so a shell processes it</p>
<pre class="lang-bash prettyprint-override"><code>kubectl exec client -n namespace -- \
  sh -c 'PASSWORD=pswd /mnt/script.sh'
</code></pre>
<p>This comes with the usual caveats around <code>kubectl exec</code>: a &quot;distroless&quot; image may not have these standard tools; you're only modifying one replica of a Deployment; your changes will be lost as soon as the Pod is deleted, which can sometimes happen outside of your control.</p>
