<p>I have a script in a pod called <code>script01</code> and it can take parameters.
I run the script through the following:</p>

<pre><code>POD=&lt;pod name&gt;
runScript="kubectl -n nmspc exec $POD -- script01"
$runScript --command "do stuff"
</code></pre>

<p>The reason I run it this way is that I don't have access to create a script on the local machine but I do have access to the script on the pod. </p>

<p>The issue is I want to pass the IP of the host machine to the pod and wanted to do it using an environment variable. I've tried using bash -c to pass the parameters but when calling the script through the variable, it doesn't append the parameters.</p>

<pre><code>runScript="kubectl -n nmspc exec $POD -- bash -c \"export curIP=123 &amp;&amp; script01\""
</code></pre>

<p>but it does work if I run it with 
<code>$runScript --command "do stuff"</code></p>

<p>How can I pass an environment variable to the pod but still be able to call the script through the variable?</p>

<p><code>/usr/bin/env</code> exports values passed in <code>key=value</code> pairs into the environment of any program it's used to invoke.</p>

<pre><code>kubectl -n nmspc exec "$POD" -- env curIP=123 script01
</code></pre>

<p>Note that you should <strong>never</strong> use <code>$runScript</code> or any other unquoted expansion to invoke a shell command. See <a href="http://mywiki.wooledge.org/BashFAQ/050" rel="noreferrer">BashFAQ #50</a> -- <em>I'm trying to put a command in a variable, but the complex cases always fail!</em></p>

<hr>

<p>As an example of how you <em>could</em> keep <code>bash -c</code> in place but have your command work, consider:</p>

<pre><code>runScript() {
  kubectl -n nmspc exec "$POD" -- bash -c 'export curIP=123 &amp;&amp; script01 "$@"' _ "$@"
}

runScript --command "do stuff"
</code></pre>

<p>Here, <code>runScript</code> is a <em>function</em>, not a string variable, and it explicitly passes its entire argument list through to <code>kubectl</code>. Similarly, the copy of <code>bash</code> started by <code>kubectl</code> explicitly passes <em>its</em> argument list (after the <code>$0</code> placeholder <code>_</code>) through to <code>script01</code>, so the end result is your arguments making it through to your final program.</p>
