Split output to two commands in bash

UPDATE 2 (2012-12-06): I think this was wrong until now, but I have it figured out. See the updated post below.

I have long wondered how to split output to two commands in bash, I figured it out today.

<br />
cat file.txt | tee &gt;(head) | tail<br />

will give both the head and the tail of the file. I have always wanted to run the output of a command through a Y into two separate commands and this is so logical that I can’t believe that I had never heard of it before

This uses the beautiful bash feature of creating a file descriptor from a programs output or input.  I love this feature and it allows for so much. Head will get the first 10 or fewer lines and tail will get the last 10 or fewer lines.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.