UNIX Tutorial Three (Extended)

3.5 Output streams stdout and stderr  

Unix systems support two output streams named stdout and stderr. The stream stdout is used for normal output and the stream stderr is used for error messages. C++ programmers know these two streams as cout and cerr respectively.

Normally, both output streams display to the screen. But, when using redirection, the streams are distinct and can be redirected independently. Let us consider how this might affect our workflow.

For example, if we compile our C++ program prog1.cpp we may get error messages on the first compile. Some types of coding errors result in a cascade of errors which scroll off the screen before we have read them. The most important error message is the first one, since that will indicate the line of code where things first start to go wrong. We would like to save these error messages to a file which we can browse conveniently.

Suppose we try to compile our program as shown below. For illustration purposes, we use a program with only one error message.

% g++ prog1.cpp

and get the messages:

prog1.cpp: In function `int main()' :
prog1.cpp:7:4: error: `a' was not declared in this scope
a = 1 ;
^

If we try to use UNIX redirection to save the error messages to a file, we might try:

% g++ prog1.cpp > msgs

we do this and see:

prog1.cpp: In function `int main()' :
prog1.cpp:7:4: error: `a' was not declared in this scope
a = 1 ;
^

and we find the file 'msgs' empty.

Why didn't we get the error messages in the file 'msgs'? The compiler (g++) writes the error messages to stderr, as that is the purpose of the stream stderr. However, the UNIX re-direction syntax '>' only redirects the stream stdout. The stream stderr is still sent to the screen. Redirection control over both streams is discussed in section 3.7 below. First, we need to introduce various UNIX shells since they use different syntax for redirection control.

3.6 The UNIX shells 'sh', 'bash', 'csh', and 'tcsh'  

In UNIX terminology, a shell is a command line interpreter that reads the commands we type, interprets them, and takes the appropriate actions. There are a number of choices for which shell you may prefer to use. In the various shells, different command syntax is sometimes required to express the desired actions. We summarize these shells as follows:

The discussion of UNIX shells in this tutorial is not intended to be all-inclusive. There are a several other shells including ash, dash, korn, zsh, et.al.

3.7 Redirecting stdout and stderr  

3.7.1 Redirecting stdout and stderr in csh and tcsh  

We take another look at our redirecting problem from section 3.5. This time we use the command:

% g++ prog1.cpp >& msgs

we do this and see:

%

We see no error messages on the screen, but we find them in the file output 'msgs'. I.e.,

% cat msgs
prog1.cpp: In function `int main()' :
prog1.cpp:7:4: error: `a' was not declared in this scope
a = 1 ;
^

3.7.2 Redirecting stdout and stderr in sh and bash  

In this section, let us assume we are using either sh or. bash. We reconsider the redirecting problem from section 3.5. The correct syntax is:

$ g++ prog1.cpp > msgs 2>&1

We see no error messages on the screen, but we find them in the file output 'msgs'. I.e.,

$ cat msgs
prog1.cpp: In function `int main()' :
prog1.cpp:7:4: error: `a' was not declared in this scope
a = 1 ;
^

The syntax used in this example may seem a bit strange. The part '> msgs' of the command above specifies that standard output is to be redirected to the file msgs. The portion of the command that reads '2>&1' is telling the shell that file descriptor 2 (stderr) is to be sent to file descriptor 1 (stdout). Therefore, both streams are redirected to the file 'msgs'.

Summary

  Output streams   stdout and stderr
  command > file   redirect standard output to a file
  command >& file   redirect standard output and standard error to a file (csh and tcsh)    
  command > file 2>&1     redirect standard output and standard error to a file (sh and bash)    

Continue with the Tutorial


M.Stonebank@surrey.ac.uk, © 9th October 2000