Broken pipe in tee with process substituion

2019-04-08 16:52发布

问题:

I just found out about process substitution using >() and am super excited about it, however when I tried it, it doesn't always work. e.g.

This works:

cat /usr/share/dict/words |tee >(tail -1) > /dev/null
ZZZ

And this gives a broken pipe error:

cat /usr/share/dict/words |tee >(head -1) > /dev/null
1080
tee: /dev/fd/63: Broken pipe

Any idea why? Thanks!

Update: This is on RHEL 4 and RHEL 6.2

回答1:

here's an explanation of why you get the error with head but not with tail:

head -1 only has to read one line of its input. then it will exit and the tee continues feeding its output into...

tail -1 on the other hand has to read the complete input in order to complete its job, so it will never terminate the pipe before tee is finished.

you can safely ignore the broken pipe message and many programs stopped reporting such errors. on my machine I don't see it.



标签: bash shell