What Is /dev/null in Linux

What Is /dev/null in Linux

What Is dev-null in Linux.webp


The /dev/null is a null device that discards any data that is written to it. However, it reports back that the write operation is successful. In UNIX terminology, this null device is also referred to as the bit bucket or black hole. When trying to read from the /dev/null, it returns an EOF character. Anything that is written to /dev/null vanishes for good.

Redirection to /dev/null in Linux

We can discard any output of a script that we use by redirecting to /dev/null.
For example, we can try discarding echo messages using this trick.
echo 'Hello from JournalDev' > /dev/null
You will not get any output since it is discarded!

There is another way of doing the same; by redirecting stderr to stdout first, and then redirect stdout to /dev/null.
The syntax for this will be:
command > /dev/null 2>&1
Notice the 2>&1 at the end. We redirect stderr(2) to stdout(1). We use &1 to mention to the shell that the destination file is a file descriptor and not a file name.

Use of /dev/null in Linux
As I mentioned earlier, it is a black hole and whatever is sent there, can not be recovered.
One of the most common use of /dev/null is to redirect either error or output of a command to this file.
Imagine a command runs for some time and it generates both standard output and errors. If you don't care about reading errors or logs. Do you redirect them to /dev/null.
 
Top