Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I wanted to know the difference between the following two commands

2>&1 > output.log 

and

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.

1  
@Niall C. Are questions regarding Unix now forbidden here? Will all Unix tags be migrated? –  David J. Liszewski Sep 10 '11 at 1:09
1  
@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

4 Answers 4

up vote 9 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 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), the outputs that to the terminal and the file.

The bottom line is that the first sends stderr/stdout to the file, the seconds 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
3  
-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:

After

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

Your Answer

 
discard

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.