What is stderr (Standard Error)?

The Standard Error (stderr) is a term used to define the line or file descriptor of the error messages for a process, script, terminal, shell, etc. Simply the stderr is used to output errors about different types of actions in computing. Generally, the Linux, Unix, and BSD operating systems use the standard error. Because the standard error is defined in the POSIX standard which is an operating system standard applied for Linux, Unix, BSD, etc.

Why Use Standard Error (stderr)?

The standard error (stderr) can be useful and used for different cases like below.

  • Get detailed error information
  • Save errors for scripts for reviewing later
  • Storing the errors for logging and log files
  • Showing errors in an interactive monitoring system

Linux Bash Standard Error (stderr)

The Linux bash shell provides the stderr in order to redirect shell and command errors. It can be used to store error messages in a file. The stderr is depicted with the descriptor number 2 for the bash and the > sign can be used to redirect this error.

First, we need to run a command which will generate errors. For example, we will use the find command and search for the filename “*wisetut*” which will generate errors like “Permission denied“.

$ find / -iname '*wisetut*'

The stderr output will be like below. By default, the stderr will be printed to the current shell.

find: ‘/snap/core18/1932/etc/ssl/private’: Permission denied
find: ‘/snap/core18/1932/root’: Permission denied
find: ‘/snap/core18/1932/var/cache/ldconfig’: Permission denied
find: ‘/snap/core18/1932/var/lib/private’: Permission denied
find: ‘/snap/core18/1885/etc/ssl/private’: Permission denied
find: ‘/snap/core18/1885/root’: Permission denied
find: ‘/snap/core18/1885/var/cache/ldconfig’: Permission denied
find: ‘/snap/core18/1885/var/lib/private’: Permission denied
find: ‘/snap/core18/1885/var/lib/snapd/void’: Permission denied

Redirect stderr To A File

We will use the 2> in order to redirect the stderr into a file named errors.txt . This will prevent the stderr errors to be displayed on bash. So all stderr generated errors will be written into the file named errors.txt. Redirecting stderr is very useful as if there are a lot of errors reading and searching them via the console or terminal is very hard and not fast. By storing stderr into a file it became very easy to read and search standard errors created by commands and tools.

$ find / -iname '*wisetut*' 2> errors.txt

Redirect stderr To Null

Alternatively, the errors can be redirected to the null. This means all generated errors or stderr will be redirected into the device /dev/null which is nothing. Redirecting stderr into null is very useful as it will not fill the console or terminal with errors or we do not need to store standard errors into a file which may take lots of storage.

$ find / -iname '*wisetut*' 2> /dev/null

Grep/Filter stderr Output Interactively

The stderr puts lots of errors about the currently running operation or command. We may look for a specific error, term, or word. We can easily grep or filter stderr output interactively by using the grep command.

$ find / -iname '*wisetut*' 2>&1 |  grep "cache"

Leave a Comment