<p>I'm trying to create a script in order to run multiple <code>kubectl exec</code> commands against multiple pods with multiple containers. The script seems to generate the command just fine but errors out when attempting to run it.</p>
<p>example command that is generated: <code>kubectl -n &lt;namespace&gt; exec &lt;pod_name&gt; -c &lt;container_name&gt; -- openssl version</code></p>
<p>When I copy the generated command and run it directly it works fine, but if I try to run the command within the script I get an error.</p>
<pre><code>OCI runtime exec failed: exec failed: unable to start container process: exec: &quot;openssl version&quot;: executable file not found in $PATH: unknown
</code></pre>
<p>command terminated with exit code 126</p>
<p>snippet from .sh file:</p>
<pre><code>for pod in $PODS; do
CONTAINERS=($(kubectl -n $NAMESPACE get pods $pod -o jsonpath='{.spec.containers[*].name}' | tr -s '[[:space:]]' '\n'))
header &quot;{pod: \&quot;$pod\&quot;, containers: \&quot;$(echo $CONTAINERS | tr -d '\n')\&quot;}&quot;

if [ &quot;$DRYRUN&quot; != &quot;true&quot; ]; then
    for container in $CONTAINERS; do
        echo &quot;COMMAND BEING RUN: \&quot;kubectl -n $NAMESPACE exec $pod -c $container -- $COMMAND\&quot;&quot;
        kubectl -n $NAMESPACE exec $pod -c $container -- $COMMAND
    done
fi
done
</code></pre>

<p>The command provided with <code>exec</code> will be executed without a shell. This means that environment variables will not be available. Thus the error specifying missing executable path in $PATH. Only executables installed in default <code>$PATH</code> (like <code>/bin/</code>) can be executed.</p>
<p>You can load the shell and provide <code>$command</code> as a command to be executed;</p>
<pre><code>kubectl exec -n $NAMESPACE --container $container -it $pod -- /bin/sh -c &quot;$COMMAND&quot;
</code></pre>
