I want to create a wrapper program that can wrapper whatever shell commands user provides, like:
./wrapper "cmd1 && cmd2"
In Python, I can call os.system("cmd1 && cmd2")
. But Golang's exec.Command
needs a list for command and args. Is there way in Golang to archive the same as Python's os.system()
?
bash
. So try to runexec.Command()
withbash -c 'cmd1 && cmd2'
os.system
takes a string and passes it to a shell, which it executes. Go has no direct support for this, but you can useos.Getenv
to fetch the value of theSHELL
environment variable, fall back to/bin/sh
if it's missing/empty, and execute it passing it the source string as the argument to the-c
shell's parameter.