This one is mostly for academic reasons. ;-)
Consider the following Bash statement:
foo | bar
This executes two processes, foo
and bar
, such that the standard-out of foo
is connected to the standard-in of bar
. So far, so good.
Is there a way to invoke foo
and bar
so that we also have a connection in the opposite direction?
foo.stdout -> bar.stdin
foo.stdin <- bar.stdout
As I understand it, Bash calls pipe(2)
to create a pair of FDs connected to each other, which can then be used to replace stdin
/ stdout
in the respective child processes. At the kernel level, I see no reason why you couldn't call pipe(2)
twice and arrange the circular arrangement depicted above.
But I can't figure out the shell syntax to actually do that.
So far, the best idea I can come up with is to use named pipes, or maybe to launch a subshell and do some crazy FD-to-FD redirecting somehow...
bash
supports only one co-process at a time. Thebash
manual mentions this under the "BUGS" heading ;-)ksh
supports more (unclear how many). – Kusalananda Jul 08 '16 at 21:37coproc
though... – MathematicalOrchid Jul 11 '16 at 07:07