1

Note this passage from man bash ( emphasis mine ):

Coprocesses

A coprocess is a shell command preceded by the coproc reserved word. A coprocess is executed asynchronously in a subshell, as if the command had been terminated with the & control operator, with a two-way pipe established between the executing shell and the coprocess.

Now, as we know unlike other *nix systems, Linux pipes are unidirectional(also ref man pipe(7), Portability section). So how does bash coproces achieve the "two-way pipe" without one existing on Linux ?

1 Answers1

4

It makes one for stdin and one for stdout, the same way pipes to subprocesses always work. That's why you get two fds in an array. One end of each pipe is in the parent (as the FDs in the array), and one end of each pipe is in the child (as fd 0 and fd 1, stdin & stdout). Writing to fd 1 in the child gives you something to read from COPROC[0], and vice-versa for standard input.

This setup is also described in the man page in the paragraph after the one you quoted. There's nothing special going on, and it does the same thing using the standard pipe function on all systems, regardless of how the local pipes work over and above what POSIX provides.

Michael Homer
  • 76,565