What Does Outputting to /dev/null Accomplish in Bash Scripts?

In Linux, /dev/null is the virtual block device which demolishes any written data due to its special properties also, known as bitbucket or blackhole. It vanishes the standard output, standard input, and standard error. This post will discuss the outputting to /dev/null in the bash script. The content for the post is as follows:

Process of Outputting to /dev/null Accomplish in Bash Scripts

In Linux, every file gives the standard input, standard output, and standard files which are represented by a unique integer (non-negative) in the operating system. The following table shows all three options for each file

0For stdin.
1For stdout.
2For stderr

“Stdin”, “stdout”, and “stderr” are the terms used for standard input, standard output, and standard error.

Let’s move and check some common usage of the/dev/null command.

/dev/null Output With Redirection Operator “>”

“>” is the redirection operator; using it with the “/dev/null” command in the bash script will stop the output of the command. Let’s understand this concept in more depth by using examples. In the below scenario, we have “echo”, which is printing some message on the screen. Still, the redirection operator “>” is redirecting its output to the /dev/null command and /dev/null command discarding its output:

echo 'Welcome to itslinuxfoss' > /dev/null

The bash script in the terminal will give no output because of /dev/null:

$ ./bash.sh

The above image shows no output.

Let’s discuss one more example for a detailed understanding.

If we type the wrong command before the re-redirection operator, it will give the error message. Do you know why? Because we are only stopping the standard output (stdout), not a standard error (stderr). Check the execution of the below command in the bash script to clear this concept:

$ touch --wrong_option > /dev/null

We have typed the wrong command in the bash script, which can be seen in the above image. Now let’s run this bash script in the terminal:

The error message has been displayed in the image.

/dev/null Output For Displaying Error Message “1”

As discussed above, the “1” option is used for the standard output. This option can be used for displaying the output of the first command. In the example, the file.txt is created, which is forwarding to the /dev/null, but instead of stopping it, the “1” option will take the output of the touch command and execute it.

$ touch file.txt 1>/dev/null

Let’s check the output of the above bash script file in the terminal:

$ ./bash.sh

The “file.txt” has been created as shown.

/dev/null Output For Discarding Error Message “2”

Similarly, the “2” option is used for discarding the error message in case of a wrong command. Let’s practice it with an example. In the below case, we have given the wrong option in the touch command, so the error message is forwarding to the /dev/null command, but the “2” option will discard the error:

$ touch -wrong_option 2>/dev/null

Let’s check this script in the terminal to see if the error message stops:

$ ./bash.sh

No error message has been displayed.

Alternative Method:

There is an alternative command that will also give the same results. In this command, we are redirecting the error message to the /dev/null command, and the next section is saying to the bash script that sends the error message (stderr) to standard output (stdout). Here “&”, option tells that destination is the file descriptor:

$ touch --wrong_option > /dev/null 2>&1

Let’s run the above script in the terminal:

$ ./bash.sh

No error message can be seen in the above image.

Note: using 2>1, you will only redirect to the (stderr), and it will create the file with name 1 and write down the error message in that file.


The output of the /dev/null in the bash script depends upon the redirection operator “>” with stdin(0), stdout(1), and stderr(2) options. This post has demonstrated the various outputs of the /dev/null with different options with the help of examples. Additionally, an alternate method for the redirection operator “>” with the stderr(2) option has also been elaborated on in this write-up.