<p>First, the yaml file is right,because I can use them directley create a mysql cluster in kubernetes.<br />
but when I try to create a mysql cluster by kubernetes api for java, an error occured<br />
The commond in yaml file cannot be recognized by the process.<br />
The key component of the yaml file is as follows</p>
<pre class="lang-yaml prettyprint-override"><code>initContainers:
      - name: init-mysql
        image: mysql:5.7.33
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-secret
              key: password
        command:
        - bash
        - &quot;-c&quot;
        - |
          set -ex
          [[ $(hostname) =~ -([0-9]+)$ ]] || exit 1
          ordinal=${BASH_REMATCH[1]}
          echo [mysqld] &gt; /mnt/conf.d/server-id.cnf
          echo server-id=$((100 + $ordinal)) &gt;&gt; /mnt/conf.d/server-id.cnf
          if [[ ${ordinal} -eq 0 ]]; then
            cp /mnt/config-map/master.cnf /mnt/conf.d
          else
            cp /mnt/config-map/slave.cnf /mnt/conf.d
          fi
        volumeMounts:
        - name: conf
          mountPath: /mnt/conf.d
        - name: config-map
          mountPath: /mnt/config-map
</code></pre>
<p>and the java code is as follows</p>
<pre class="lang-java prettyprint-override"><code>........
........
.withInitContainers(new V1ContainerBuilder()
        .withName(&quot;init-mysql&quot;)
        .withImage(&quot;mysql:5.7.33&quot;)
        .withEnv(env)
        .withCommand(&quot;bash&quot;,
                     &quot;\&quot;-c\&quot;&quot;,
                     &quot;|&quot;,
                     &quot;set -ex&quot;,
                     &quot;[[ $(hostname) =~ -([0-9]+)$ ]] || exit 1&quot;,
                     &quot;ordinal=${BASH_REMATCH[1]}&quot;,
                     &quot;echo [mysqld] &gt; /mnt/conf.d/server-id.cnf&quot;,
                     &quot;echo server-id=$((100 + $ordinal)) &gt;&gt; /mnt/conf.d/server-id.cnf&quot;,
                     &quot;if [[ ${ordinal} -eq 0 ]]; then&quot;,
                     &quot;  cp /mnt/config-map/master.cnf /mnt/conf.d&quot;,
                     &quot;else&quot;,
                     &quot;  cp /mnt/config-map/slave.cnf /mnt/conf.d&quot;,
                     &quot;fi&quot;
                     )
         .withVolumeMounts(new V1VolumeMountBuilder()
         .withName(&quot;conf&quot;)
         .withMountPath(&quot;/mnt/conf.d&quot;)
         .build(),
         new V1VolumeMountBuilder()
         .withName(&quot;config-map&quot;)
         .withMountPath(&quot;/mnt/config-map&quot;)
         .build()
         )
  .build(),
  ......
  ......
</code></pre>
<p>The java code and the yaml file look the same, but when I execute it, an error occured. The result for kubectl logs is as follows</p>
<pre class="lang-bash prettyprint-override"><code>bash: &quot;-c&quot;: No such file or directory
</code></pre>
<p>so I think it may be caused by the uncorrect params of withCommand function.<br />
How can I fix it.Thank you.</p>

<p>In your Java code, you are explicitly including double quotes in the string, <code>withCommand(&quot;bash&quot;, &quot;\&quot;-c\&quot;&quot;, ...)</code>.  That's causing the container command to execute something similar to <code>bash '&quot;-c&quot;' ...</code> where the quotes are part of the argument.  In turn, that doesn't start with a hyphen, so bash interprets it as a script to run, but when there isn't a local file named exactly <code>&quot;-c&quot;</code> including the quotes as part of the filename, you get that error.</p>
<p>The answer to your immediate question is just to remove those extra quotes</p>
<pre class="lang-java prettyprint-override"><code>.withCommand(&quot;bash&quot;,
             &quot;-c&quot;,   // no extra quotes here
             ...)
</code></pre>
<p>There's a second half of your question about the YAML syntax: why does what you show work?  I believe it would also work to remove the double quotes here</p>
<pre class="lang-yaml prettyprint-override"><code>command:
  - bash
  - -c
  - ...
</code></pre>
<p>The trick here is that YAML has three different kinds of inline strings (&quot;flow scalars&quot;).  <a href="https://yaml.org/spec/1.2.2/#733-plain-style" rel="nofollow noreferrer">Plain strings</a> have no quotes but allow no escaping either; <a href="https://yaml.org/spec/1.2.2/#732-single-quoted-style" rel="nofollow noreferrer">single-quoted strings</a> have <code>'single quotes'</code> but very limited escaping options; and <a href="https://yaml.org/spec/1.2.2/#731-double-quoted-style" rel="nofollow noreferrer">double-quoted strings</a> have <code>&quot;double quotes&quot;</code> and also can represent any string through escaping.  <a href="https://yaml.org/spec/1.2.2/#81-block-scalar-styles" rel="nofollow noreferrer">Block scalars</a> provide a further kind of string for multi-line text.</p>
<pre class="lang-yaml prettyprint-override"><code>These are all the same:
  - the same
  - 'the same'
  - &quot;the same&quot;
  - &quot;the\u0020same&quot;
  - &gt;-
    the same
</code></pre>
<p>In some cases you need a particular quoting style to work around YAML syntax issues.  In your case, the <code>-c</code> option looks somewhat similar to the YAML <code>- item</code> list syntax (though the list syntax <em>requires</em> a space after the hyphen) so perhaps the original YAML author chose to quote it to make it unambiguous.  It shouldn't be required here though.</p>
<p>The YAML parser will remove the quotes before the application sees them.  In the Kubernetes case, this means the quotes around <code>&quot;-c&quot;</code> are removed at the YAML layer and the actual command list contains <code>[bash, -c, ...]</code> with no quotes.</p>
