<p>I've a perl command which I need to pass to a kubernetes job. Command computes and prints the value of pi to 2000 places.</p>

<p>perl -Mbignum=bpi -wle 'print bpi(2000)'</p>

<p>I've passed the command to a job yaml file as shown below. Via kubectl, yaml file creates job successfully and prints the value of pi. </p>

<pre><code>apiVersion: batch/v1
kind: Job
metadata:
   name: pi-job
spec:
   template:
     spec:
        containers:
          - name: pi
            image: perl
            command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
        restartPolicy: Never
</code></pre>

<p>When I tried with below command syntax, it gives me error. Could you please tell me why the below command synax is incorrect? </p>

<pre><code>command: ["perl",  "-Mbignum=bpi", "-wle", "'print bpi(2000)'"]     # placed inside single quote
command: ["perl",  "-Mbignum=bpi", "-wle", "print", " bpi(2000)"]   # print splitted in two quotes.
command: ["perl",  "-Mbignum=bpi", "-wle", "print", "bpi(2000)"]    # print splitted in two quotes.
</code></pre>

<p>Also when I pass the complete command in a single quote, it gives me error.</p>

<pre><code>command: ["perl -Mbignum=bpi -wle print bpi(2000)"]       # complete command in single quotes.
</code></pre>

<p><code>command:</code> key requires that you pass <strong>an array</strong> as it's argument (compare with <a href="https://rollout.io/blog/yaml-tutorial-everything-you-need-get-started/" rel="nofollow noreferrer">this</a> example). You can specify an array in two equivalent forms:</p>

<p>in a single line, using square brackets <code>[]</code>:</p>

<blockquote>
<pre><code>---
items: [ 1, 2, 3, 4, 5 ]
names: [ "one", "two", "three", "four" ]
</code></pre>
</blockquote>

<p>and in a multi-line fashion:</p>

<blockquote>
<pre><code>---
items:
  - 1 
  - 2 
  - 3
  - 4 
  - 5 
names:
  - "one"
  - "two"
  - "three"
  - "four"
</code></pre>
</blockquote>

<p>You can use both double <code>"</code> and signle <code>'</code> quotes, so the example below will be also correct and will work:</p>

<pre><code>command: ['perl',  '-Mbignum=bpi', '-wle', 'print bpi(2000)']
</code></pre>

<p>However you cannot use both at the same time. I don't know what you would like to achieve by putting there <code>"'print bpi(2000)'"</code>, but this syntax doesn't make any sense and is simply incorrect.</p>

<p>You might as well ask why you can't run <code>'echo bla'</code> in <code>bash</code> while at the same time <code>echo bla</code> runs successfully, giving you the desired result.</p>

<p>Note that when providing <code>command</code> this way in <strong>kubernetes</strong>, only first element of the array is an actual <code>command</code> (executable file searched in <code>$PATH</code>) and other following elements are its arguments. Keeping this in mind, you should notice that your next two examples also don't make any sense as neither "print" nor "bpi(2000)" provided separately are not valid arguments:</p>

<blockquote>
<pre><code>command: ["perl",  "-Mbignum=bpi", "-wle", "print", " bpi(2000)"]   # print splitted in two quotes.
command: ["perl",  "-Mbignum=bpi", "-wle", "print", "bpi(2000)"]    # print splitted in two quotes.
</code></pre>
</blockquote>

<p>To be able to fully understand what this command is doing, we need to dive a bit into basic <code>perl</code> documentation. I left only those options that were used in our example and are relevant to it:</p>

<pre><code>$ perl --help

Usage: perl [switches] [--] [programfile] [arguments]
  -e program        one line of program (several -e's allowed, omit programfile)
  -l[octal]         enable line ending processing, specifies line terminator
  -[mM][-]module    execute "use/no module..." before executing program
  -w                enable many useful warnings

Run 'perldoc perl' for more help with Perl.
</code></pre>

<p>Now let's analyse our command step by step:</p>

<pre><code>["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
</code></pre>

<p>Each element of the array, which follows the main command <code>"perl"</code> is a separate entity which is passed as <em>an argument of the main command</em>, <em>its flag</em>, or <em>an argument of recently provided flag</em>, which according to documentation is required and should be provided in a very specific form.</p>

<p>In our <code>-wle</code> set of flags, <code>e</code> option is crucial as it must be followed by specific argument:</p>

<pre><code> -e program        one line of program (several -e's allowed, omit programfile)
</code></pre>

<p>which in our example is:</p>

<pre><code>print bpi(2000)
</code></pre>

<p>I want to emphasize it again. Every element of the array is treated as a separate entity. By breaking it into two separate elements such as <code>"print", " bpi(2000)"</code> or <code>"print", "bpi(2000)"</code> you feed <code>perl -e</code> only with <code>print</code> argument which doesn't make for it any sense as it requires very specific command telling it what should be printed. It is exactly as if you run in bash shell:</p>

<pre><code>perl -Mbignum=bpi -wle 'print' 'bpi(2000)'
</code></pre>

<p>which will result in the following error from perl interpreter:</p>

<pre><code>Use of uninitialized value $_ in print at -e line 1.
</code></pre>

<p>And finally when you run your last example:</p>

<blockquote>
  <p>command: ["perl -Mbignum=bpi -wle print bpi(2000)"]       # complete
  command in single quotes.</p>
</blockquote>

<p>you'll get quite detailed message explaining why it doesn't work in <code>Pod</code> events (<code>kubectl describe pod pi</code>):</p>

<pre><code>Error: failed to start container "pi": Error response from daemon: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \"perl -Mbignum=bpi -wle print bpi(2000)\": executable file not found in $PATH": unknown
</code></pre>

<p>It basically tries to find in $PATH an executable file named <code>"perl -Mbignum=bpi -wle print bpi(2000)"</code> which of course cannot be done.</p>

<p>If you want to familiarize with different ways of defining a <code>command</code> and its <code>arguments</code> for a <code>container</code> in <strong>kubernetes</strong>, I recommend you reading <a href="https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/" rel="nofollow noreferrer">this</a> section in official kubernetes documentation.</p>
