Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

Comments

  1. exec.Command takes a shell command and optional arguments passed to the shell command, e.g., exec.Command("ls", "-lha"). It does not work if you pass a shell command with arguments as a single string to exec.Command, e.g., exec.Command("ls -lha") does not work. There are a few situations where it is appealing to pass a shell command with arguments as a single string to exec.Command.

    • You want to run a compound shell command which leverages pipe (|), &&, ||, etc. Even though you can break such a compound shell command into multiple smaller ones and then run each of them using exec.Command, it can be much more convenient to be able to run a compound shell command directly.

    • Use provides a shell command as a single string for your Golang application to run.
      It might not be trivia to parse a string shell command to its array representation.

    Fortunately, there is one way around this problem. You can explicitly call a shell and pass a command (with arguments) to it, e.g., exec.Command("bash", "-c", "ls -lha").

import "fmt"
import "reflect"
import "os"
import "os/exec"
cmd := exec.Command("ls")
cmd
/usr/bin/ls
reflect.TypeOf(cmd)
*exec.Cmd
err := cmd.Run()
err
err == nil
true
out, err := exec.Command("ls").Output()
err == nil
true
out
[116 105 112 115 45 111 110 45 103 111 108 97 110 103 46 105 112 121 110 98 10]
string(out[:])
tips-on-golang.ipynb

Compound Command

exec.Command("ls", "|", "wc", "-l").Output()
[] exit status 2
exec.Command("bash", "-c", "ls -lha")
/usr/bin/bash -c ls -lha

Environment Variables

cmd = exec.Command("bash", "-c", "ls -lha")
/usr/bin/bash -c ls -lha
os.Environ()
[PATH=/usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/jvm/java-8-openjdk-amd64/bin LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64 M2_HOME=/usr/share/maven JUPYTERHUB_API_TOKEN=f53fdbc5460c44ffb1bf695cca4caa28 JPY_API_TOKEN=f53fdbc5460c44ffb1bf695cca4caa28 JUPYTERHUB_ADMIN_ACCESS=1 JUPYTERHUB_CLIENT_ID=jupyterhub-user-dclong JUPYTERHUB_HOST= JUPYTERHUB_OAUTH_CALLBACK_URL=/user/dclong/oauth_callback JUPYTERHUB_OAUTH_SCOPES=["access:servers!server=dclong/", "access:servers!user=dclong"] JUPYTERHUB_USER=dclong JUPYTERHUB_SERVER_NAME= JUPYTERHUB_API_URL=http://127.0.0.1:8081/hub/api JUPYTERHUB_ACTIVITY_URL=http://127.0.0.1:8081/hub/api/users/dclong/activity JUPYTERHUB_BASE_URL=/ JUPYTERHUB_SERVICE_PREFIX=/user/dclong/ JUPYTERHUB_SERVICE_URL=http://127.0.0.1:34125/user/dclong/ JUPYTERHUB_ROOT_DIR=/workdir JUPYTERHUB_DEFAULT_URL=/lab USER=dclong HOME=/home/dclong SHELL=/bin/bash JUPYTERHUB_SINGLEUSER_APP=jupyter_server.serverapp.ServerApp GIT_PYTHON_REFRESH=quiet PYTHONUNBUFFERED=1 JPY_PARENT_PID=70]
cmd.Env = append(os.Environ(),
		"FOO=duplicate_value", // ignored
		"FOO=actual_value",    // this value is used
	)

Get Detailed Error Message

Please refer to How to debug “exit status 1” error when running exec.Command in Golang for detailed discussions.

os.Getenv("PATH")
/usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/jvm/java-8-openjdk-amd64/bin
os.LookupEnv("PATH")
/usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/jvm/java-8-openjdk-amd64/bintrue