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 - Logout Command
The Linux logout command is a quick utility that allows you to log out of your present terminal session. It's similar to the action of clicking "Log Out" on the desktop of a computer, but for the terminal. It's particularly handy if you're using a login shellâa special terminal session associated wi
4 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 - Set Command
The `set` command in shell scripting is a powerful tool that used for controlling the behavior of the shell and the environment in which scripts run. It allows the users to modify the shell options and positional parameters which facilitates providing greater control over script execution and debugg
6 min read
Shell Script Examples
For all the Linux distributions, the shell script is like a magic wand that automates the process, saves users time, and increases productivity. This shall scripting tutorial will introduce you to the 25 plus shall scripting examples. But before we move on to the topic of shell scripting examples, l
15+ 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 - Shopt Command
Unlike many shell commands, shopt is a bash-only shell command. It is a built-in BASH shell command that controls certain options for the shell session. One can consider this as a boolean ON/OFF switch for certain shell options. The âshoptâ command provides control over many settings that are used t
7 min read
Shell Scripting - Bash Trap Command
The trap command in Bash executes specific code in response to the signal given to the program. It traps the signal received by the program from the OS or the user and then executes the specific code provided by the programmer. It acts like a trigger in response to a signal. Just like try-catch stat
5 min read
Shell Scripting - Subshell
Shell scripting is a powerful tool for automating tasks and simplifying the management of systems and applications. One important concept in shell scripting is the use of subshells, which allow you to execute commands within a separate shell environment. A subshell is a child shell that is spawned b
4 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