What is /dev/null You Often View in the Commands and Shebang Scripts We Supply You? What is More Confusing Looking is /dev/null 2>&1? Get the Full Idea From This Article. Full idea means, within the capability of an online informative webpage. Some basics are mandatory to know to completely understand the usage and we have hyperlinked towards our own articles wherever possible. What are not hyperlinked and the Phrase Appears Like Classical Chinese (obviously if the human does not know the Language!) !
What Are Taken as Granted You Already Know While Explaining What is /dev/null
We are taking it granted that the reader has a minimum knowledge about :
- Linux
- UNIX
- Unix Like OS
- POSIX
- Real Time Operating System
- SSH
- Shell
- Terminal Emulation
- Shebang Script or BASH Scripting
- Unix Pipes
- stdout
- stderr
One Dozen is very less “syllabus”. It is very easy to ask what is /dev/null but it is very difficult to reply in short. That definitely never makes the question bad!
---
What is /dev/null – Funny Explanation
If you are science student, definitely you have heard of null. That null has lot of meanings. In math, the Null Sign [ ∅ ] indicates an empty set. In Physics, Null Point is often used as a terminology. It is usually means zero (reminded me of Wheatstone bridge!). So, practically in general; null means nothing physical. There is a Sanskrit Sloka – Hari Om Tat Sat. It literally means “all that is”. Our /dev/null
is somewhat like that. Huge resemblance – both has four components if the backslashes are counted!
Hari is the highest human or para-human, exactly, “/” is the highest location.
Om is a scared sound, exactly, “dev” is a great word ! This dev is for Device. Virtual Device. Dev in Sanskrit means God.
Tat is a secret thing. “/” is also kind of secret – on an on exiting device, we are using unix path like thing.
Sat is also a secret thing. There is Legal terminology – Null and Void.
In short, /dev/null
means God Knows!
What is /dev/null – The Real Thing
/dev/null is a virtual device file NOT directory, this file; /dev/null is standardized by the POSIX standard. OS X, all Linux, BSD etc. are practically POSIX Compliant Operating System. It is derived from UNIX null device, used as a data sink. In short, /dev/null is a special file in Unix-like operating systems that discards all data written to it. > /dev/null
redirects the standard output ( stdout
) towards the /dev/null file, which discards it. Redirecting means hiding the output. If there is 2>&1
, like > /dev/null 2>&1
, then it means the redirection is towards the standard error (2 ( stderr
) ) to standard output (1 ( stdout
) ), which then discards this as well, because the standard output has already been redirected. Even if the command writes to stderr
, that output will be silent. 2>&1
redirects stderr
to stdout
.
stdin
is 0.stdout
is 1.stderr
is 2.>
is direction to be written.
Know these :
1 2 3 | 0 = standard input (/dev/stdin) 1 = standard output (/dev/stdout) 2 = standard error output (/dev/stderr) |
The &
is used for construction purpose.
1 | ProgA 2>&1 | ProgB |
the standard error is duplicated before the pipe on the standard output.
There is no meaning of knowing about Other Operating Systems equivalent stuffs. Dev Null is also a virtual reality character!
What is /dev/null – Practical Usages
Without usage examples, the concept will go to your head’s /dev/null
location! Yes, you can cd
to /dev
on OS X :
Original UNIX reference :
1 | http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap10.html |
You should know about the unix wheel group.
1 2 3 4 5 6 | program 2>/dev/null program >/dev/null 2>&1 program1 2>/dev/null | program2 ln -sf /dev/null ~/.bash_history ssh example.com "program </dev/null >/dev/null 2>&1 &" dd if=/dev/cdrom of=/dev/null |
Inside a script, there can be commands which can throw error like this if ran manually :
1 | bash : Permission denied |
That thing inside script will make the whole stuff useless. We sent to God and silenced the problem. One piece of practical example :
1 2 3 4 5 6 7 8 9 | #!/bin/bash # This script will automatically create new user read -p "Type name of the new user : " username empty_check $username if id -u $username >/dev/null 2>&1; then echo "User exists, please select another username" exit 1 fi |
This is hugely used for cron :
1 | @hourly /foo/bar/script.backup >/dev/null 2>&1 |
For clean scripts, usage of /dev/null
is actually limited.