Previous Table of Contents Next

Module 134


The external tee command is used to write to the standard output and to a file simultaneously. You place the tee command anywhere in a pipe command to divert a copy of the standard input (of tee) to disk and another copy to your terminal. Thus its name is from the world of plumbing; it allows one input and two outputs. The tee command actually allows multiple file copies. For example,

     sort unsorted | tee sorted | cut -c-20,41- | tee cutup | lp

sorts the data in the file unsorted; tee then sends a copy to the file sorted. The standard output of tee is piped to cut which removes columns 21 through 40 and pipes it to another tee. The second tee writes a copy to the file cutup and pipes a copy to lp for printing.


Following is the general format of the tee command.

     tee [ -ai ] file_list


The following options may be used to control how tee functions.

-a Appends the output to an existing file. If the file does not exist it will be created. Normally an existing file would be overwritten.
-i Ignore interrupts. If you press the Delete key, tee ignores the interrupt signal sent to it and continues processing.


The following argument may be passed to the tee command.

file_list One or more files where tee writes copies of the input.


The tee command reads from the standard input and writes to the standard output and any files listed on the command line.


You can use the tee command to execute a command interactively (in foreground) and save a copy of the output on disk. The tee command can be placed anywhere in a pipe to check what the output looks like at a certain point. Thus it lends itself to debugging pipe commands and capturing output for later perusal.


In this activity you use the tee command to receive input from the sort command, send the output to the printer, and store a copy in a file. Begin at the shell prompt.

1.  Type nawk -F: '{ print $1}' /etc/passwd | sort | tee users | lp and press Return. Notice the message returned by lp. You now have a printout on the default printer. The output is also stored in the file named users.

BSD (Berkeley)
Type nawk -F: '{ print $1}' /etc/passwd | sort | tee users | lpr and press Return.

2.  Type cat users and press Return to display the contents of the users file.
3.  Turn to Module 116 (BSD) to continue the learning sequence.

Previous Table of Contents Next