[Solved] Complex command in exec.Command()


You don’t have to use the shell for redirection, you can just let Go do it:

package main

import (
   "os"
   "os/exec"
)

func main() {
   f, e := os.Create("requirements.txt")
   if e != nil {
      panic(e)
   }
   defer f.Close()
   c := exec.Command(
      "docker", "exec", "-it", "demoContainer", "bash", "-c", "pip freeze",
   )
   c.Stdout = f
   c.Run()
}

solved Complex command in exec.Command()