Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang: Executing a command with it's arguments

I'm trying to execute a command using go.

executableCommand := strings.Split("git commit -m 'hello world'", " ")
executeCommand(executableCommand[0], executableCommand[1:]...)
cmd := exec.Command(command, args...)

But here is what I get

error: pathspec 'world"' did not match any file(s) known to git.
exit status 1

This is because -m gets 'hello only and not 'hello world' since the command line is split using " ".

Any idea to make it work?

like image 501
Alex. b Avatar asked Jul 14 '16 13:07

Alex. b


1 Answers

What you want is actually hard to achieve without help of shell that interprets quotes etc. So you may use shell to run your command.

exec.Command("sh", "-c", "echo '1 2 3'")
like image 99
Grzegorz Żur Avatar answered Oct 15 '22 14:10

Grzegorz Żur