43.9. How to tee Several Commands into One Place
The tee (Section 43.8) command writes its standard input to a file and writes the same text to its standard output. You might want to collect several commands' output and tee them all to the same file, one after another. The obvious way to do that is with the -a option:
$ some-command | tee teefile $ another-command | tee -a teefile $ a-third--command | tee -a teefile
A more efficient way is:
$ (some-command > another-command > a-third-command) | tee teefile
The subshell operators (Section 43.7) collect the standard output of the three commands. The output all goes to one tee command. This has two differences from the first method. First, you need two fewer pipes, two fewer tees, and one more subshell. Second, you can pipe the output of the single tee command to another process -- for example, to print it.
Unfortunately, the C shell doesn't make this quite as easy. If you can type all the commands on one line, you can do it this way (the same thing works in the Bourne shell):
% (command1; command2; command3) | tee teefile
Otherwise, use a semicolon and backslash (;\) at the end of each line:
% (some-command ;\ another command ;\ a-third-command) | tee teefile
In all these examples, remember that if you don't need to see the output of the commands, you don't need tee. Use the subshell as above, but replace | tee teefile with > outfile or | somecommand.
Copyright © 2003 O'Reilly & Associates. All rights reserved.