Think of/dev/null as a "black hole". It is very equivalent to a write-only file. All content written to it is lost forever. And trying to read content from it is nothing. However,/dev/null is very useful for both the command line and the script.
Prohibit standard output.
1 Cat $filename >/dev/null
2 # The file content is missing and will not be exported to standard output.
Prohibit standard error
1 RM $badname 2>/dev/null
2 # This error message [standard error] was thrown into the Pacific Ocean.
Suppresses standard output and standard error output.
1 Cat $filename 2>/dev/null >/dev/null
2 # if "$filename" does not exist, there will be no error message prompts.
3 # if "$filename" exists, the contents of the file are not printed to standard output.
4 # So therefore, the code above does not output any information at all.
5 # It is useful when you want to test only the exit code of a command without any output.
6 #-----------The exit begin----------------------of the test command #
7 # ls dddd 2>/dev/null 8 8 # echo $? Output command exit code: 0 for the command to execute correctly, 1-255 for error. 9 #-----------The exit end-----------of the test command #
10# Cat $filename &>/dev/null 11 # can also be noted by Baris Cicek.
Clear Log File contents
1 Cat/dev/null >/var/log/messages
2 #: >/var/log/messages has the same effect, but will not produce a new process. (because: is built)
3
4 Cat/dev/null >/var/log/wtmp
Example 28-1. Hide cookies and no longer used
1 if [f ~/.netscape/cookies] # if present, delete.
2 Then
3 Rm-f ~/.netscape/cookies
4 fi
5
6 Ln-s/dev/null ~/.netscape/cookies
7 # Now all cookies will be thrown into the black hole and not stored on the disk.