The "tee" command

Printer-friendly version

Again, I answered a question on Usenet: Can someone explain how tee command works

The tee(1) ("man 1 tee") command copies everything from it's standard input to both it's standard output, and to each file named in it's arguments.

So, the pipeline

echo "Hello there" | tee output_file | sed 's/there/here/'

will

  • echo the text "Hello there" to stdout, which is piped into tee's stdin
  • which will copy the text "Hello there" from stdin to the file called output_file, and to stdout, which is piped into sed's stdin
  • which will read the text from stdin, change the first occurrence of the string "there" to "here", and send all the read data to stdout
  • where the resulting "Hello here" text will display on your screen

resulting in

  • the file output_file containing the text "Hello there", and
  • the text "Hello here" showing on the screen.


~/tmp $ ls
~/tmp $ echo "Hello there" | tee output_file | sed 's/there/here/'
Hello here
~/tmp $ ls
output_file
~/tmp $ cat output_file
Hello there
~/tmp $
Articles: