I wanted to know the difference between the following two commands 2>&1 > output.log and 2>&1 | tee output.logI 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?
Difference between 2>&1 > output.log and 2>&1 | tee output.log
shell;io redirection;pipe
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.txtwhere nothing ends up on the terminal.