<p>I am currently trying to execute a simple bash command onto my kubernetes pod but seem to be getting some errors which does not make sense.</p>
<p>If I exec into the docker container an run the command plain</p>
<pre><code>I have no name!@kafka-0:/tmp$ if [ $(comm -13 &lt;(sort selectedTopics) &lt;(sort topics.sh) | wc -l) -gt 0 ];  then  echo &quot;hello&quot;; fi
</code></pre>
<p>I get Hello as output.</p>
<p>But if execute the same from the outside as</p>
<pre><code>kubectl exec --namespace default kafka-0 -c kafka -- bash -c &quot;if [ $(comm -13 &lt;/tmp/selectedTopics &lt;/tmp/topics.sh| wc -l) -gt 0 ];  then  echo topic does not exist &amp;&amp; exit 1; fi&quot;
</code></pre>
<p>Then I get an error message stating that  <code>/tmp/topics.sh: No such file or directory</code></p>
<p>event though I able to do this</p>
<pre><code>kubectl exec --namespace $namespace kafka-0 -c kafka -- bash -c &quot;cat /tmp/topics.sh&quot;
</code></pre>
<p>why does kubectl <code>exec</code> causing me problems?</p>

kafka
<p>When you write:</p>
<pre><code> kubectl ... &quot;$(cmd)&quot;
</code></pre>
<p><code>cmd</code> is executed on the local host to create the string that is used as the argument to <code>kubectl</code>.  In other words, you are executing <code>comm -13 &lt;/tmp/selectedTopics &lt;/tmp/topics.sh| wc -l</code> on the local host, and not in the pod.</p>
<p>You should use single quotes if you want to avoid expanding locally:</p>
<pre><code>kubectl exec --namespace default kafka-0 -c kafka -- bash -c 'if comm -13 &lt;/tmp/topics.sh grep -q . ;  then  echo topic does not exist &gt;&amp;2 &amp;&amp; exit 1; fi'
</code></pre>
