Tomáš Pospíšek's Notizblock

piping to multiple processes

Searching the web brought me to this nice (and broken) article.

In short, if you want to split stdout and pipe it into multiple processes you can do:

echo bar | tee >( grep b > /tmp/foo1 ) >( grep ba > /tmp/foo2 )

What does this do?

First, tee does not only allow to write its stdin to a file and in parallel output it to stdout as in:

( do stuff ) | tee /tmp/log-file | grep ...

but allows to output it to multiple files as in:

( do stuff ) | tee /tmp/log1 /tmp/log2 | grep ...

That's the first trick. The second trick is bash's >( ) operator.

Let's take the initial example (and simplify it):

echo bar | tee >( grep ba > /tmp/foo1 )

What the >( ) operator does is:

  1. it creates a named pipe.

On my machine that named pipe looks something like:

/dev/fd/63
  1. it connects that named pipe to the stdin of the subshell that bash created (I'm assuming it is creating a subshell...).

In other words it does something like

cat /dev/fd/63 | grep ba > /tmp/foo1
  1. and then it replaces the whole >( stuff ) thing with the name of the named pipe.

Which gives:

echo bar | tee /dev/fd/63

Voilà. Now you can pipe into multiple subshells.

Once again, this blew my mind. I didn't know bash can do this. Bash's man page doesn't waste any needles words on this magnificent feature and covers it in 9 lines. Given bash's 6175 lines of man page there must be unconveivable quantities of unimaginable features still waiting for me to discover ... 8-O!

Tomáš Pospíšek, 2020-09-29

Articles