I am trying to run command with exec.Cmd(command, flags...) and want to have the flexibility to modify the arguments before calling the cmd.Run() function.
for example:
cmd := exec.Command("echo", "hello world")
cmd.Env = []string{"env1=1"}
cmd.Args = []string{"echo2", "oh wait I changed my mind"}
cmd.Run()
The above code seems to be always running echo hello world but not echo2 oh wait I changed my mind
Am I correct to expect echo2 to be run instead of echo?
exec.Command, the first parameter is the command, subsequent parameters are the args. So your code changes the actual execution to (shell equivalent)echo "echo2" "oh wait I changed my mind". To change the command to execute, not just its args, you have to updatecmd.Path.