Take the 2-minute tour ×

The command causing the failure,

gpc xxx.pas > error.txt

Because I want to upload the error message to the stackflow.com,but it turns out there is nothing in the error.txt,moreover the gpc still cried to the stdout and print the error message one the screen?

So why would shell redirection fail?


share|improve this question

1 Answer 1

up vote 5 down vote accepted

STDERR (standard error) in UNIX and UNIX-like systems is redirected using 2> instead of a single chevron (>).


gpc xxx.pas 2> error.txt

This will allow standard output (STDOUT) to be printed to the terminal like normal, but send all error messages to the file, error.txt.

If you want to collect both STDOUT and STDERR in the same file, use 2>&1: this tells the shell to copy STDERR (2) to STDOUT (1). Now redirecting STDOUT gets both streams:

gpc xxx.pas 2>&1 > stdout-and-stderr.txt
share|improve this answer
@John T: thanks a lot. –  Jichao Nov 3 '09 at 2:30
+1 good answer. ;) –  quack quixote Nov 3 '09 at 2:33
I'll add that programs can output content to both STDOUT and STDERR. They don't often choose wisely. Consider what the program would output by default. For a compiler, I consider programming errors to be "output"; REAL errors would be stuff like "filesystem full; cannot write file" or "file not found" or "library corrupt" or stuff like that. So it's pretty sad that a programming error goes out on STDERR. Note that the redirection directives are different for csh/tcsh than from bash which is what is given in the example above. –  pbr Nov 13 '09 at 1:04
@John T: please replace "UNIX and UNIX-like systems" by "/bin/sh and Bourne shell-like shells" - csh and derivatives dosn't support this. –  reinierpost Apr 23 '10 at 12:27

Your Answer


