Stderr

Updated: 09/12/2023 by Computer Hope

Stderr, also known as standard error, is the default file descriptor where a process can write error messages.

Standard error illustration

In Unix-like operating systems, such as Linux, macOS X, and BSD (Berkeley Software Distribution), stderr is defined by the POSIX (portable operating system interface for Unix) standard. Its default file descriptor number is 2.

In the terminal, standard error defaults to the user's screen.

Stderr on the Linux command line

In bash, standard error can be redirected on the command line. Redirecting stderr can be useful if you need to capture any error messages to a separate log file or hide the error messages entirely.

For example, consider the following find command:

 find / -iname '*something*'
 /usr/share/doc/something
 /usr/share/doc/something/examples/something_random
 find: `/run/udisks2': Permission denied
 find: `/run/wpa_supplicant': Permission denied
 /usr/share/something
 /usr/games/something

We're getting errors because find is trying to search a few system directories we don't have permission to read. The lines that say "Permission denied" are error messages and were written to stderr. The other lines were written to stdout (standard output). By default, they both display on the terminal.

To hide stderr, we can redirect them by referencing standard error's file descriptor number, 2, and a "redirect output" operator, >.

 find / -iname '*something*' 2>/dev/null
 /usr/share/doc/something
 /usr/share/doc/something/examples/something_random
 /usr/share/something
 /usr/games/something

The errors are no longer displayed because all output to stderr was redirected to /dev/null, the special device in Linux that "goes nowhere."

We could also redirect all errors to a file:

 find / -iname '*something*' 2>output.txt
 /usr/share/doc/something
 /usr/share/doc/something/examples/something_random
 /usr/share/something
 /usr/games/something

Now, if you cat the contents of output.txt, you'll see the error messages were saved there:

 cat output.txt
 find: `/run/udisks2': Permission denied
 find: `/run/wpa_supplicant': Permission denied

For detailed information about redirecting data streams, see redirection in bash.

Error, Error message, Operating System terms