Search code examples
goexec

golang exec.Command returns exit status 1 with bash


I am trying to run multiple commands using bash -c in exec.Command, when i manually run the command, it returns no error the output of command is nil but it's okay. I don't know why it returns exit status 1 when i run it through golang exec.Command.

Here is my code :

cmd := exec.Command("bash", "-c", "blkid -o device | grep -v part | grep /dev/mapper")
_ = cmd.Wait()
stdout, err := cmd.Output()

I even tried this answer from another question, but it also didn't worked out. The important thing is that when i run command on a vm where output is not nil the command executes successfully, but when i tried this on Centos where output is nil it failed.


Solution

  • This is because you have to pass the command in the string slice. Use this instead -

    cmd := exec.Command("bash",strings.Fields("-c blkid -o device | grep -v part | grep /dev/mapper"))
    stdout, err := cmd.Output()