HomeLinuxWhat Is /Dev/Null

What Is /Dev/Null


The /dev/null is likely one of the mostly used particular digital units on Linux. On this information, we are going to focus on about it and showcase the varied methods of utilizing it.

What Is /Dev/Null in Linux

Linux treats the whole lot as a file, be it a driver or a tool. The /dev listing is used to retailer all of the bodily and digital units. In the event you labored with disk partitioning, you could have seen the /dev listing in use. For instance: /dev/sda, /dev/sdb1, and so on.

Every of the particular digital units include distinctive properties. For instance, studying from /dev/zero returns the ASCII NUL characters. Among the well-liked digital units embody:

  • /dev/null
  • /dev/zero
  • /dev/random
  • /dev/urandom

The /dev/null is a null gadget that discards any knowledge that’s written to it. Nevertheless, it reviews again that the write operation is profitable. In UNIX terminology, this null gadget can be known as the bit bucket or black gap.

Utilizing /Dev/Null in Linux

Properties of /Dev/Null
When attempting to learn from the /dev/null, it returns an EOF character:

Regardless of its distinctive options, /dev/null is a legitimate file. We are able to confirm it utilizing the next command:

Primary Utilization
Something that’s written to /dev/null vanishes for good. The next instance demonstrates this property:

$ echo “hiya world” > /dev/null
$ cat /dev/null

Right here, we redirect the STDOUT of the echo command to the /dev/null. Utilizing the cat command, we learn the content material of the /dev/null.

Since /dev/null doesn’t retailer any knowledge, there’s nothing within the output of the cat command.

Redirecting the Command Outputs to /Dev/Null

Must run a command with out coping with its output? We are able to redirect the info to the /dev/null to discard safely. To implement this system, it requires prior information of file descriptors: STDIN, STDOUT, and STDERR.

Instance 1:
Try the primary instance:

$ echo “hiya world” > /dev/null

Right here, we redirect the STDOUT of the echo command to /dev/null. That’s why it gained’t produce any output within the console display.

Instance 2:
Try the subsequent instance:

Right here, the asdfghjkl command doesn’t exist. So, Bash produces an error. Nevertheless, the error message didn’t get flushed to /dev/null. It’s as a result of the error message is saved in STDERR. So, we have to specify the STDERR (2) redirection as effectively. Right here’s the up to date command:

Instance 3:
What if we wish to redirect each STDOUT and STDERR to /dev/null? The command construction would appear to be this:

$ <command> > /dev/null 2> /dev/null

Whereas this construction is totally legitimate, it’s verbose and redundant. There’s a technique to shorten the construction dramatically: redirecting STDERR to STDOUT first, then redirecting STDOUT to /dev/null.

Try the up to date command:

$ <command> > /dev/null 2>&1

Right here, STDOUT is redirected to /dev/null. Then, we redirect the STDERR (2) to STDOUT (1). The “&1” describes to the shell that the vacation spot is a file descriptor, not a file title.

Now, the command is extra environment friendly to implement.

Conclusion

We realized about /dev/null, the null gadget in Linux. We additionally demonstrated the way it operates utilizing numerous examples. These strategies are broadly utilized in shell scripting.

Curious about studying extra? Try the Bash programming sub-category that comprises quite a few guides on numerous ideas of Bash scripting.

Completely happy computing!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments