Golang os/exec usage (notes)
Golang os/exec usage (notes) Add extra environment variables before running a command cmd := exec.Command("programToExecute") additionalEnv := "FOO=bar" newEnv := append(os.Environ(), additionalEnv) cmd.Env = newEnv out, err := cmd.CombinedOutput() if err != nil { log.Fatalf("cmd.Run() failed with %s\n", err) } fmt.Printf("%s", out) Use os/exec with pipes or bash expressions on Linux rcmd := `iw dev | awk '$1=="Interface"{print $2}'` cmd := exec.Command("bash", "-c", rcmd) out, err := cmd.CombinedOutput() if err != nil { log.Println(err.Error()) } log.Println(string(out)) Use os/exec with batch expressions on Windows cmd := exec.Command("cmd", "/c", "ffmpeg -i myfile.mp4 myfile.mp3 && del myfile.mp4") out, err := cmd.CombinedOutput() if err != nil { log.Println(err.Error()) } log.Println(string(out)) Fix local language issues in Windows CMD with os/exec cmd := exec.Command("cmd", "/c", "chcp 65001 && netsh WLAN show drivers") out, err := cmd.CombinedOutput() if err != nil { log.Println(err.Error()) } log.Println(string(out)) When using this method, remember to remove the first line of output (the chcp response) before further processing.