I was experimenting with redirections and pipes and did not understand some behaviour. I have this snippet to generate one line on each of stderr and stdout:
(echo stdout; echo stderr 1>&2)
It seems to work correctly, (try >/devnull and 2>/dev/null).
But the behaviour of these three commands confuses me:
(echo stdout; echo stderr 1>&2) > >(wc -l)
stderr
1
(echo stdout; echo stderr 1>&2) 2> >(wc -l)
stdout
1
(echo stdout; echo stderr 1>&2) > >(wc -l) 2> >(wc -l)
2
(empty)
Why does the last command combine the two streams? Or what else is happening to break my brain?
because the second wc -l
derives stdout
from current command (stdout already redirect to fist wc -l
), its output is also passed to first wc -l
.
IN +-----------+ 1> +---------+ OUT
+-+--->echo stdout+----+----> wc -l +------------->
| +-----------+ ^ +---------+
| |
| |
| +<------------------+
| |
| |
| +-----------+ 2> +---------+ |
+--->echo stderr+---------> wc -l +----+
+-----------+ +---------+