I am using the tee command to output the compilation errors of a program into a file along with the terminal.

gcc hello.c | tee file.txt 

This is the command I have used. The compilation errors are displayed on the terminal but they are not outputted in the file. How should I output the std errors into file?

  • OK, good. Any question ? – Gilles Quenot Jan 7 '15 at 9:53
  • tee only redirects stdout, compilation errors are presumably being output to stderr. See this post for more info on redirecting stderr with tee – Josh Jolly Jan 7 '15 at 9:59

With csh, tcsh, zsh or recent versions of bash, try

gcc hello.c |& tee file.txt


  • |& instruct the shell to redirect standard error to standard output.

In other Bourne-like shells:

gcc hello.c 2>&1 | tee file.txt

In rc-like shells:

gcc hello.c >[2=1] | tee file.txt

In the fish shell:

gcc hello.c ^&1 | tee file.txt
| improve this answer | |
  • This is a bash 4 feature. For older versions of bash, use 2>&1 | instead. – Ansgar Esztermann Jan 7 '15 at 10:07
  • @AnsgarEsztermann, that's a (t)csh feature, also available in zsh and recently added to bash. – Stéphane Chazelas Jan 7 '15 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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