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 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
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.
$ find / -iname '*wisetut*' 2> errors.txt
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.
$ find / -iname '*wisetut*' 2> /dev/null