some_command | tee >(command1) >(command2) >(command3) ... | command4

Use tee + process substitution to split STDOUT to multiple commands

Using process substitution, we can 'trick' tee into sending a command's STDOUT to an arbitrary number of commands. The last command (command4) in this example will get its input from the pipe.
Sample Output
$ echo foo | tee >(cat) >(cat) >(cat) | cat
foo
foo
foo
foo

23
By: SiegeX
2009-02-17 01:55:07
tee

These Might Interest You

  • Concatenate the stdout of multiple commands. Show Sample Output


    2
    cat <( command1 arg arg ) <( command2 arg ) ...
    Pistos · 2009-03-07 04:33:12 1
  • Most of the "most used commands" approaches does not consider pipes and other complexities. This approach considers pipes, process substitution by backticks or $() and multiple commands separated by ; Perl regular expression breaks up each line using | or < ( or ; or ` or $( and picks the first word (excluding "do" in case of for loops) note: if you are using lots of perl one-liners, the perl commands will be counted as well in this approach, since semicolon is used as a separator Show Sample Output


    4
    history | perl -F"\||<\(|;|\`|\\$\(" -alne 'foreach (@F) { print $1 if /\b((?!do)[a-z]+)\b/i }' | sort | uniq -c | sort -nr | head
    alperyilmaz · 2010-04-08 13:46:09 1
  • Tee can be used to split a pipe into multiple streams for one or more process to work it. You can add more " >()" for even more fun. Show Sample Output


    33
    echo "tee can split a pipe in two"|tee >(rev) >(tr ' ' '_')
    axelabs · 2010-08-14 20:38:59 3
  • { ... } vs ( ... ) reduces the process count, I believe. Typically, I use this logic to redirect a series of commands to a pipe -- { command1 args1 ; command2 args2}|less -- or file -- { command1 args1 ; command2 args2}>foo -- or to spawn the series as a background process -- { ... } & .


    0
    { command1 args1 ; command2 args2 ; ... }
    IF_Rock · 2011-04-12 20:01:51 0
  • Useful to recover a output(stdout and stderr) "disown"ed or "nohup"ep process of other instance of ssh. With the others options the stdout / stderr is intercepted, but only the first n chars. This way we can recover ALL text of stdout or stderr Show Sample Output


    3
    strace -e write=1,2 -p $PID 2>&1 | sed -un "/^ |/p" | sed -ue "s/^.\{9\}\(.\{50\}\).\+/\1/g" -e 's/ //g' | xxd -r -p
    glaudiston · 2010-10-06 19:37:39 1
  • Bash has a great history system of its commands accessed by the ! built-in history expansion operator (documented elsewhere on this site or on the web). You can combine the ! operator inside the process redirection Very handy. Show Sample Output


    7
    <(!!)
    drewk · 2010-02-06 18:35:10 2

What Others Think

Thanks for this one.
CodSpirit · 478 weeks and 5 days ago
Yet another confirmation that tee is awesome. This one in my favourites.
rbossy · 446 weeks and 3 days ago
That one doesn't work for me, it is not portable: /bin/mksh: syntax error: '(' unexpected
Natureshadow · 308 weeks and 6 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: