I encountered an interesting issue when I wanted to show first and last N lines of a relatively large file, say 15K lines. I used the following command,
tee >(head) >(tail) <bigfile.txt >/dev/null
It reads reasonable and makes sense, however this command didn’t execute as I expected, the data was cutoff. tail didn’t return the last N lines but some lines in the file which was also the end of what tee spat before the command failed with exit status 141. It seems to be 128 + 13 (SIGPIPE).
The problem lies with head command and probably Bash, not entirely sure. But here is my theory, head ends when it finishes its job, that is printing out first N lines, it exits itself with no errors. However, tee and tail hasn’t finished yet, the file is too big to process in short time. As soon as head ends, Bash cleans up the Process Substitution of head, then throws out the error when tee tries to write more data.
I am pretty sure that tee isn’t the one throw error, because if it can’t write to a file description successfully, it simply skips that file description in next writes and continues its work, it won’t exit abruptly. So, this only leaves Bash, but just my guessing.
A workaround is to make sure the process doesn’t end before all data is given.
tee >(sed -n '1,10p') >(tail) <bigfile.txt >/dev/null
For a small file, a few dozens of lines, head may not cause any trouble, but you can test with this:
echo | tee >(:)
It will definitely return exit status 141.