Silencing the Output of a Bash Command
2.1. Standard Output & Error
Bash also automatically opens multiple files for each process, denoted by a numeric File Descriptor (FD).
Two of these FDs are Standard output (stdout) and standard error (stderr). By default, Bash directs the error stream to stderr and the output stream to stdout. For both stdout and stderr, any characters written to these FDs are displayed in the console where the command was executed.
Additionally, Bash assigns the identifier 1 for the stdout FD and 2 for the stderr FD.
2.2. Redirecting Output
We can change the default destination of error and output streams by redirecting the output of our command to another FD. We redirect output using the redirection operator: >.
For example, we can write Hello world to the file foo.txt using the echo command:
echo "Hello world" > foo.txt
3. Silencing Output
3.1. Standard Output
command 1> /dev/null
By default, the redirection operator redirects stdout so we can omit the 1:
command > /dev/null
3.3. All Output
We combine this with redirecting stdout to /dev/null to silence all output:
command > /dev/null 2>&1
Thus, stdout is redirected to /dev/null and stderr is redirected to stdout, causing both streams to be written to /dev/null and silencing all output from our command.
We can shorten this to the following Bash notation:
command &> /dev/null
Note that this shorthand is not portable and is only supported by Bash 4 or higher.
Although not common, we can also separately redirect stdout and stderr to /dev/null, but we do not suggest this approach unless we are auto-generating a Bash script or the previous approaches cannot be used:
command > /dev/null 2> /dev/null
By combining redirection with the /dev/null device, we can silence error output, normal output, or both.