<p>So I've tried to execute a chain i.e. multiple commands on a Pod's container using <code>client-go</code>, and it seems to only work for some commands like <code>ls</code>.</p>

<p>Here is what I've tried:</p>

<pre><code>    req := client.CoreV1().RESTClient().Post().Resource("pods").Name(pod.Name).Namespace(pod.ObjectMeta.Namespace).SubResource("exec") // .Param("container", containerName)
    scheme := runtime.NewScheme()
    if err := _v1.AddToScheme(scheme); err != nil {
        panic(err.Error())
    }
    parameterCodec := runtime.NewParameterCodec(scheme)
    req.VersionedParams(&amp;_v1.PodExecOptions{
        Stdin:     false,
        Stdout:    true,
        Stderr:    true,
        TTY:       false,
        Container: containerName,
        Command:   strings.Fields("/bin/sh -c " + command),
    }, parameterCodec)
    exec, err := remotecommand.NewSPDYExecutor(restConfig, "POST", req.URL())
    if err != nil {
        panic(err.Error())
    }
    var stdout, stderr bytes.Buffer
    err = exec.Stream(remotecommand.StreamOptions{
        Stdin:  nil,
        Stdout: &amp;stdout,
        Stderr: &amp;stderr,
        Tty:    false,
    })
    if err != nil {
        panic(err.Error())
    }
log.Printf("Output from pod: %v", stdout.String())
log.Printf("Error from pod: %v", stderr.String())
</code></pre>

<p>When the <code>command</code> variable is just a simple <code>ls -l</code>, I get the desired output. But when I try to do something like <code>'ls -l &amp;&amp; echo hello'</code> it produces an error <code>command terminated with exit code 2</code>. 
It doesn't output anything if I only put <code>echo hello</code>. However, it does produce the desired output <code>hello</code> if I remove the <code>Bourne Shell</code> prefix <code>/bin/sh -c</code> and the <code>Command</code> attribute equals to <code>string.Fields("echo hello")</code>, but this approach doesn't let me chain commands. </p>

<p>All in all, what I am trying to do is to execute a chain of commands on a Pod's container.</p>

Gigaxel
<p>The <code>corev1.PodExecOptions.Command</code> accept value of <code>[]string</code> type.</p>

<pre class="lang-golang prettyprint-override"><code>req.VersionedParams(&amp;_v1.PodExecOptions{
  Stdin:     false,
  Stdout:    true,
  Stderr:    true,
  TTY:       false,
  Container: containerName,
  Command:   cmds,
}, parameterCodec)
</code></pre>

<p>where, <code>cmds</code> can be:</p>

<pre class="lang-golang prettyprint-override"><code>cmds := []string{
    "sh",
    "-c",
    "echo $HOME; ls -l &amp;&amp; echo hello",
    }
</code></pre>

<p>Output:</p>

<pre><code>/root
total 68
drwxr-xr-x   2 root root 4096 Feb 24 00:00 bin
drwxr-xr-x   2 root root 4096 Feb  1 17:09 boot
drwxr-xr-x   2 root root 4096 Feb 24 00:00 mnt
drwxr-xr-x   2 root root 4096 Feb 24 00:00 opt
dr-xr-xr-x 396 root root    0 Mar 19 11:47 proc
drwx------   2 root root 4096 Feb 24 00:00 root
.
.
hello
</code></pre>

<p><strong>Explanation:</strong> <a href="https://stackoverflow.com/a/33888424/11032044">Tim's answer</a></p>

<pre><code>command: ["/bin/sh","-c"]
args: ["command one; command two &amp;&amp; command three"]
</code></pre>

<p>The command <code>["/bin/sh", "-c"]</code> says <strong>"run a shell, and execute the following instructions"</strong>. The args are then passed as commands to the shell. In shell scripting a <code>semicolon</code> separates commands, and <code>&amp;&amp;</code> conditionally runs the following command if the first succeed. In the above example, it always runs command <code>one</code> followed by command <code>two</code>, and only runs command <code>three</code> if command <code>two</code> succeeded.</p>

<p><strong>N.B.:</strong>
For bash, it will be similar to something like below:</p>

<pre class="lang-golang prettyprint-override"><code>cmds := []string{
     "bash",
     "-c",
     `export MYSQL_PWD=${MYSQL_ROOT_PASSWORD}
mysql -h localhost -nsLNE -e "select 1;" 2&gt;/dev/null | grep -v "*"`,
                    },
</code></pre>
