bash - How can I send the stdout of one process to multiple processes using (preferably unnamed) pipes in Unix (or Windows)? -
i'd redirect stdout of process proc1 2 processes proc2 , proc3:
proc2 -> stdout / proc1 \ proc3 -> stdout
i tried
proc1 | (proc2 & proc3)
but doesn't seem work, i.e.
echo 123 | (tr 1 & tr 1 b)
writes
b23
to stdout instead of
a23 b23
editor's note:
- >(…)
process substitution nonstandard shell feature of some posix-compatible shells: bash
, ksh
, zsh
.
- written, answer accidentally sends output process substitution's output through pipeline too.
- echo 123 | tee >(tr 1 a) >(tr 1 b) >/dev/null
prevent that, has pitfalls: output process subsitutions unpredictably interleaved, and, except in zsh
, pipeline may terminate before commands inside >(…)
do.
in unix (or on mac), use tee
command:
$ echo 123 | tee >(tr 1 a) | tr 1 b b23 a23
usually use tee
redirect output multiple files, using >(...) can redirect process. so, in general,
$ proc1 | tee >(proc2) ... >(procn-1) | procn
will want.
under windows, don't think built-in shell has equivalent. microsoft's windows powershell has tee
command though.
Comments
Post a Comment