Shell Scripting - /dev/null
Last Updated :
02 Aug, 2022
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.
Similar Reads
Shell Scripting - Disown Command Shell is a command language interpreter. It is used in most of the UNIX/LINUX-based distros. Shell support scripting means we can automate the Shell commands by writing some scripts with different logic, looping, and decision making. This benefits us to automate some time-consuming and tedious jobs.
5 min read
Shell Scripting - Define #!/bin/bash A shell provides an interface to connect with the system. When we use an operating system, we indirectly interact with the shell. While using a terminal every time on any Linux distribution system, we interact with the shell. The main function of the shell is to interpret or analyze Unix commands. A
3 min read
Shell Scripting - True Command A shell provides an interface with the help of which users can interact with the system easily. To directly interact with a shell, we use an operating system. On a Unix-based operating system, every time we write a command using a terminal, we interact with the system. To interpret or analyze Unix c
3 min read
Shell Scripting - Restricted Shell Shell is one of the most important and powerful tools available in GNU/Linux-based systems. One can control the entire system if used correctly, this exact feature makes the shell a double-edged sword. It can potentially harm the system if one executes a system command without knowing its underlying
5 min read
Introduction to Linux Shell and Shell Scripting If we are using any major operating system, we are indirectly interacting with the shell. While running Ubuntu, Linux Mint, or any other Linux distribution, we are interacting with the shell by using the terminal. In this article we will discuss Linux shells and shell scripting so before understandi
8 min read
Shell Scripting - Interactive and Non-Interactive Shell A shell gives us an interface to the Unix system. While using an operating system, we indirectly interact with the shell. On Linux distribution systems, each time we use a terminal, we interact with the shell. The job of the shell is to interpret or analyze the Unix commands given by users. A shell
3 min read