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 +----+
+-----------+ +---------+
It is something like
$ { echo stdout; echo stderr 3>&2 2>&1 1>&3 >&2 | wc -l; } | wc -l
2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.