If I understand correctly, you're looking for the equivalent of tee file1 file2 file3
, but rather than write the same data to three files file1
, file2
and file3
, you want to pipe the same data into three commands cmd1
, cmd2
and cmd3
, i.e.
… | ??? cmd1 cmd2 cmd3
should be equivalent to
… | cmd1 &
… | cmd2 &
… | cmd3 &
except that …
would only be executed once.
There are two ways to do that.
Ksh93, bash and zsh support process substitution. This is a generalization of pipes that allows the argument of a command to be a file that, when written to, passes data as input to a command (there is also the input variant which, when read from, obtains data output by a command). That is,
echo hello | tee >(cmd1)
prints hello
to standard output and in addition runs cmd1
with hello
as input.
So for example, if you want to duplicate the input of somecommand
and pass it to both cmd1
and cmd2
, you can use
somecommand | tee >(cmd1) | cmd2
If your shell doesn't support process substitution, you can use named pipes instead. See Arcege's answer for how that works. Named pipes are less convenient than process substitution because you have to create them and delete them, and start and synchronize processes manually. They have the advantage of being fully portable, whereas not all shells support process substitutions. They can also be used in scenarios other than the ones process substitution is for.
Under the hood, on some systems, process substitution uses named pipes internally. On most systems, though, it relies on named files representing file descriptors.