Linux_>/dev/null

ref: http://dongwei.iteye.com/blog/322702

 

You may often see: >/dev/null 2>&1  in the shell

 

The result of the command can be used to define the output in the form of %>

 

/dev/null represents an empty device file

> represents where to redirect to, for example: echo "123" > /home/123.txt

1 means stdout standard output, the system default value is 1, so ">/dev/null" is equivalent to "1>/dev/null"

2 means stderr standard error

& means equal to, 2>&1, means that the output redirection of 2 is equivalent to 1

 

Then the meaning of 1>/dev/null 2>&1:

1>/dev/null first means that the standard output is redirected to an empty device file, that is, no information is output to the terminal, and to put it bluntly, no information is displayed.

2>&1 Then, standard error output redirection is equivalent to standard output, because standard output has been redirected to an empty device file before, so standard error output is also redirected to an empty device file.

 

 

About /dev/null and its purpose

ref: http://blog.csdn.net/kaiwii/article/details/7308729

 

Think of /dev/null as a "black hole". It is very equivalent to a write-only file. Everything written to it is lost forever. Trying to read from it will read nothing. However, /dev /null is very useful for both the command line and scripts.

 

Suppress standard output.

cat $filename >/dev/null # The contents of the file are lost without output to standard output.

 

Suppress standard errors

rm $badname 2>/dev/null #The error message [standard error] is thrown into the Pacific Ocean.

 

Suppress stdout and stderr.

cat $filename 2>/dev/null >/dev/null

# If "$filename" does not exist, there will be no error message.

# If "$filename" exists, the contents of the file will not be printed to standard output.

# Therefore, the above code will not output any information at all.

# Useful when you just want to test the exit code of a command and don't want any output.

#-------------Exit begin of test command ----------------------#

# ls dddd 2>/dev/null 8 

# echo $? //Output command exit code: 0 means the command is executed normally, 1-255 means there is an error.  

#------------Test command exit end------------#  

# cat $filename &>/dev/null 

# Also, as pointed out by Baris Cicek.

 

Clear log file contents

cat /dev/null > /var/log/messages # : > /var/log/messages has the same effect, but does not spawn a new process. (Because : is built-in)

cat /dev/null > /var/log/wtmp

 

Example 28-1. Hiding a cookie instead of using it again

 

if [ -f ~/.netscape/cookies ] # Delete if exists.

 then

   rm -f ~/.netscape/cookies

be

 

ln -s /dev/null ~/.netscape/cookies

# Now all cookies will be black holed and not saved on disk.

 

ref: http://blog.csdn.net/lizhi200404520/article/details/7270151

What is the difference between command > file 2>file and command > file 2>&1 .

First of all, command > file 2>file means to send the standard output information generated by the command and the wrong output information to the file. In this way of writing command > file 2>file, both stdout and stderr are directly sent to the file, and the file will It is opened twice, so that stdout and stderr will overwrite each other, so writing is equivalent to using two pipes of FD1 and FD2 to preempt the file at the same time.

The command >file 2>&1 sends stdout directly to file. After stderr inherits the FD1 pipeline, it is sent to file. At this time, file is only opened once, and only one pipeline FD1 is used. Contains the contents of stdout and stderr.

In terms of IO efficiency, the efficiency of the previous command is lower than that of the latter command, so when writing shell scripts, we often use the writing method command > file 2>&1.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327094838&siteId=291194637