Patricio Treviño

Patricio Treviño

Husband . Father . Developer

Redirecting streams to the /dev/null sink

Redirects the stdout/stderr (or both) to a special location called /dev/null.

The /dev/null device is a special file (not a directory) and it's typically used as a sink for unwanted output streams of a process, or as a convenient empty file for input streams. This is usually done by redirection.

Why is this important? Well imagine you have a very important process that cannot not be interrupted, redirecting outputs is a good way to avoid unhandled errors.

Syntax

<operation> [n]> /dev/null [options]
view raw syntax.text hosted with ❤ by GitHub

Option Description
operation The operation whose output will be redirected.
n The stream to be redirected (see below).
options The stream redirection options.

Valid values for [n]: 1, standard out 2, standard error &, both

Example

# no redirection
$ echo 'hello'
hello
# redirects standard error (no error in this case)
$ echo 'hello' 2> /dev/null
hello
# redirects standard out to /dev/null (so no output)
$ echo 'hello' 1> /dev/null
# redirects everything to /dev/null (so no output)
$ echo 'hello' &> /dev/null
# no redirection (causing error)
$ unlink unexisting-file.sh
unlink: unexisting-file.sh: No such file or directory
# redirects standard error to /dev/null (so no error)
$ unlink unexisting-file.sh 2> /dev/null
# redirects everything to /dev/null (so no error)
$ unlink unexisting-file.sh &> /dev/null
view raw example.sh hosted with ❤ by GitHub

References

Null device