Linux/Debian - What does 'pee' in moreutils do?

6,193

Solution 1

Here's what you can do with pee:

seq 5 -1 1 > file
cat file |pee 'sort -u > sorted' 'sort -R > unsorted'

So pee works with shell pipes instead of files.

bash doesn't need pee, it can open shell commands as files:

cat file |tee >(sort -u > sorted) >(sort -R > unsorted)

Solution 2

It's probably easier to understand if you've used tee first. This useful old tool takes standard input and writes out to multiple files, plus standard output. The following:

echo "Hello world" | tee one two

Will create two files, named one and two, both containing the string Hello world. It will also print to your terminal.


Now pee performs a similar function but instead of redirecting output to multiple files it redirects to multiple secondary commands, ala pipes. It differs slightly from tee in the respect that it doesn't send the original stdin to stdout because it wouldn't make sense combining it with the output of the secondary commands. The following very simple example:

echo "Hello world" | pee cat cat

Will output the string Hello world to your terminal twice. This is because each of the two instances of cat receives the standard output and does what cat does, which is print.

Share:
6,193
Araejay
Author by

Araejay

Updated on September 17, 2022

Comments

  • Araejay
    Araejay almost 2 years

    I recently discovered the 'moreutils' package in Debian (and Ubuntu). It's a collection of convenient unix tools.

    One of the commands is 'pee'. The man page says:

    pee is like tee but for pipes.

    However it's a short man page, I have filed a bug about it. Does anyone know what it does, how to use it, why one would use it?

  • user2910702
    user2910702 over 11 years
    @Arioch, the bash syntax won't work with the hooks-joker technique. Just install moreutils.
  • Alex
    Alex over 10 years
    Use tee >/dev/null to avoid replicating stdin to stdout
  • underrun
    underrun almost 10 years
    the real advantage to pee over tee is that it sends the stdout from each sub process to the stdout of pee itself. with tee you need to redirect each processes stdout to a file if you want to save it, but with pee you just need to save the stdout. of course that only makes sense if each sub command is outputting a similarly formatted thing. like if you couldn't figure out how to or regular expressions in grep you could do cat file | pee 'grep this' 'grep that' > lines.with.this.or.that.txt ... using tee you would just get a copy of file on stdout.
  • Vlastimil Ovčáčík
    Vlastimil Ovčáčík almost 5 years
    The >(command) feature is called Process substitution.
  • Sridhar Sarnobat
    Sridhar Sarnobat about 4 years
    I'm trying to read that last example with a straight face and failing miserably