I want to emulate a bash pipe using go's os/exec module. Here's a dummy example in bash:
$ ls | wc
42 48 807
How can I emulate that in Go? Is there a way to do it with streams?
I want to emulate a bash pipe using go's os/exec module. Here's a dummy example in bash:
$ ls | wc
42 48 807
How can I emulate that in Go? Is there a way to do it with streams?
Via Brad Fitzpatrick, here's one way to do it. You can reassign the Stdin
property of the second command to the stdout
writer from the first command.
ls := exec.Command("ls")
wc := exec.Command("wc")
lsOut, _ := ls.StdoutPipe()
ls.Start()
wc.Stdin = lsOut
o, _ := wc.Output()
fmt.Println(string(o))