$ program [arguments...] 2>&1 | tee outfile
2>&1
dumps the stderr and stdout streams.
tee outfile
takes the stream it gets and writes it to the screen and to the file "outfile".
This is probably what most people are looking for. The likely situation is some program or script is working hard for a long time and producing a lot of output. The user wants to check it periodically for progress, but also wants the output written to a file.
The problem (especially when mixing stdout and stderr streams) is that there is reliance on the streams being flushed by the program. If, for example, all the writes to stdout are not flushed, but all the writes to stderr are flushed, then they'll end up out of chronological order in the output file and on the screen.
It's also bad if the program only outputs 1 or 2 lines every few minutes to report progress. In such a case, if the output was not flushed by the program, the user wouldn't even see any output on the screen for hours, because none of it would get pushed through the pipe for hours.
Update: The program unbuffer
, part of the expect
package, will solve the buffering problem. This will cause stdout and stderr to write to the screen and file immediately and keep them in sync when being combined and redirected to tee
. E.g.:
$ unbuffer program [arguments...] 2>&1 | tee outfile
foo > output
the data is written to stdout and stdout is the file namedoutput
. That is, writing to the file is writing to stdout. You are asking if it is possible to write both to stdout and to the terminal. – William Pursell