-1

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?

Kevin Burke
  • 61,194
  • 76
  • 188
  • 305
  • exact duplicate of [How to pipe several commands?](http://stackoverflow.com/questions/10781516/how-to-pipe-several-commands) – Dave C Jul 27 '15 at 01:19

1 Answers1

2

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))
Kevin Burke
  • 61,194
  • 76
  • 188
  • 305