stderr
stdout
coding
programming tips
error handling

How can I pipe stderr, and not stdout?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

When working with command line interfaces in Unix, Linux, or Unix-like operating systems, you often want to manipulate the output from the commands being executed. By default, output streams are divided into two main types: standard output (stdout) and standard error (stderr). Each serves different purposes; stdout is the primary output of commands, while stderr is intended for error messages and diagnostics. Sometimes, it is useful to redirect only the stderr stream to either a file or another command, leaving stdout untouched. This may be required for debugging purposes or for logging error information without disrupting the output flow.

Understanding File Descriptors

In Unix-like systems, file descriptors are small integers that the system uses to handle files and streams. By convention:

  • File descriptor 0 is standard input (stdin).
  • File descriptor 1 is standard output (stdout).
  • File descriptor 2 is standard error (stderr).

How to Redirect stderr

The general method for redirecting stderr involves the use of the number 2 (the file descriptor for stderr) followed by a redirection operator and the target location. Here are some examples:

Redirecting stderr to a File

To redirect stderr to a file, you can use the following command syntax:

bash
command 2> error.log

This command will direct all the error messages from command into a file named error.log.

Redirecting stderr to stdout

If you want to treat stderr the same as stdout, perhaps to pipe both to another command, you can redirect stderr to stdout using:

bash
command 2>&1

This will not redirect stdout; it only redirects stderr to the same place where stdout is currently directed.

Piping stderr to Another Command

Sometimes, you may want to process the stderr stream through another command for further handling or analysis. Here’s how you can pipe stderr, but not stdout:

bash
command 2>&1 >/dev/null | another_command

In this example, command's stderr is merged into stdout, and then stdout is redirected to /dev/null (a special file that discards all data written to it). Consequently, another_command receives only the original stderr.

Practical Example

Suppose you have a script that outputs both regular messages and error messages, and you want to write a log file containing only the error messages. You might use:

bash
./script.sh > /dev/null 2> script-errors.log

Here, stdout is discarded and only stderr is redirected to script-errors.log.

Summary Table of Redirection Commands

Command ExampleDescription
command 2> fileRedirects stderr to file.
command > file 2>&1Redirects both stdout and stderr to file by merging stderr into stdout.
command 2>&1 > fileMerges stderr into stdout but redirects only stdout to file. stderr does not end up in file.
command 2>&1 >/dev/nullMerges stderr into stdout and discards stdout.

Additional Considerations

  • Order of Redirection Matters: The order in which you write these redirections can affect the outcome significantly. For instance, command 2>&1 >file does not work as one might expect if attempting to redirect both streams into file.
  • Redirecting in Scripts: These redirections can be used not only in interactive shells but also inside shell scripts to control output flow.
  • Portability: While these redirection techniques are standard across most Unix-like systems, always check the specifics if you're working in a less common shell or a very old Unix variant.

Understanding how to redirect stderr independently of stdout allows for much more precise control over command output in scripting and system administration. This capability is essential for effective log management, error tracking, and system automation.


Course illustration
Course illustration

All Rights Reserved.