Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I wanted to know the difference between the following two commands

2>&1 > output.log 


2>&1 | tee output.log

I saw one of my colleague use second option to redirect. I know what 2>&1 does, my only question is what is the purpose of using tee where a simple redirection ">" operator can be used?

share|improve this question

migrated from stackoverflow.com Sep 10 '11 at 22:33

This question came from our site for professional and enthusiast programmers.

@Niall C. Are questions regarding Unix now forbidden here? Will all Unix tags be migrated? – David J. Liszewski Sep 10 '11 at 1:09
@unhillbilly: this has nothing to do with Unix anyways, I use bash on Windows and this applies to bash there as well! – André Caron Sep 10 '11 at 5:02
up vote 14 down vote accepted

2>&1 >output.log means first start sending all file handle 2 stuff (standard error) to file handle 1 (standard output) then send that to the file output.log. In other words, send standard error and standard output to the log file.

2>&1 | tee output.log is the same with the 2>&1 bit, it combines standard output and standard error on to the standard output stream. It then pipes that through the tee program which will send its standard input to its standard output (like cat) and also to the file. So it combines the two streams (error and output), then outputs that to the terminal and the file.

The bottom line is that the first sends stderr/stdout to the file, while the second sends it to both the file and standard output (which is probably the terminal unless you're inside another construct which has redirected standard output).

I mention that last possibility because you can have stuff like:

(echo hello | tee xyzzy.txt) >plugh.txt

where nothing ends up on the terminal.

share|improve this answer
-1 You have the syntax right, but not the semantics. Run cat /doesnotexist 2>&1 >output.txt - you will see see cat: /doesnotexist: No such file or directory displayed to the terminal and output.txt is an empty file. Order of precedence and closure are in play: 2>&1 (dup fd2 from the current fd1), then >output.txt (redirect fd1 to output.txt, not changing anything else). The reason that 2>&1 | is different is because of order of precedence: | before >. – Arcege Sep 10 '11 at 23:33

The former outputs only to the file. The second outputs both to the file and to the screen.

share|improve this answer

First command will do the another task:


2>&1 > output.log 

the old STDOUT will be saved (copied) in STDERR and then STDOUT will be redirected to file.

So, stdout will go to file and stderr will go to console.

And in

 2>&1 | tee output.log

both streams will be redirected to tee. Tee will duplicate any input to its stdout (the console in your case) and to file (output.log).

And there is another form of first:

    > output.log  2>&1

this will redirect both STDOUT and STDERR to the file.

share|improve this answer

The reason for 2>&1 | tee is to be able to capture both stdout and stderr to a log file and to see it on the screen at the same time. This could be done as >output.txt 2>&1 & tail -f as well, but you wouldn't know when the backgrounded command terminated - is the program terminated or is it running with no output. The 2>&1 | tee was a common idiom for programmers.

share|improve this answer

Here is a post summarizing Unix output streams: http://www.devcodenote.com/2015/04/unix-output-streams.html

A snippet from the post:

There are 3 standard output streams:

STDIN - Standard Input - Writes from an input device to the program
STDOUT - Standard Output - Writes program output to screen unless specified otherwise.
STDERR - Standard Error Output - Writes error messages. Also printed to the screen unless specified otherwise.
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.