Open In App

Shell Scripting – /dev/null

Last Updated : 02 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Anyone who starts with bash scripting will definitely come across /dev/null. It is a special type of file that every Linux distro has.  It is a blackhole of the operating system where anything put into it is discarded. In this article, we will be going through the /dev/null Shell Scripting command with its usage and understanding the command in a more detailed way with the help of examples. 

When we see the properties of this file using the ‘ls’ command we see something like this:

 

As seen in the above output, /dev/null has a file type of ‘c’ which means a special file known as a character device file. This file can act like a device and accept streams of data.

To know more about /dev/null file, let us understand streams. In computers, a stream is a continuous flow of data. When working with files, we use 3 types of streams:

  • stdin – input stream
  • stdout – output stream
  • stderr – error stream if any occurs

Whenever we work with files or run a command, Linux uses these 3 streams. Each stream is redirected to a file which is recognized through their respective file descriptors. For the input file, it is 0, for the output file it is 1 and for the error file, it is 2. These file descriptors are used to recognize the files and use the data sent there.

Usage of /dev/null

Example 1: Error Stream

Now let us understand their use by running a simple ping command whose output is shown below.

 

We can see both the successful as well as unsuccessful ping requests are shown in the output. What if according to our use case we want only those requests which were successful. For this case, we can use the file descriptor as shown below.

 

Example 2: Output Stream

In the above example, we redirected the output from the stderr stream to the /dev/null file which consumed the error requests, and thus only the successful pings were printed. If we were to only print the unsuccessful ping requests, we can do so by redirecting only the stdout stream to /dev/null as shown below.

 

Example 3: Discard error messages

So, the special file /dev/null is used to discard unwanted output streams of a process. Let us look at another example where we want to locate a file in the Linux system using the find command. When this command is run we get the following output.

 

But in the above example, we are also getting unnecessary error outputs giving permission errors from some directories which are very undesirable. In that case, we can redirect those error messages to the null file getting clean output as shown below.

 

Example 4: Redirect all output to /dev/null

Finally, there can be cases when we just need to execute a command and redirect all output irrespective of success or error to the /dev/null file. The below command demonstrates this.

 

Here initially we redirect all output to the null file using 1>/dev/null and then redirect stderr to stdout (which is already redirected to /dev/null) using 2>&1.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads