>/dev/null 2>&1

shell中可能常常能看到:>/dev/null 2>&1

命令的结果能够经过%>的形式来定义输出

/dev/null 表明空设备文件
> 表明重定向到哪里,例如:echo "123" > /home/123.txt
1 表示stdout标准输出,系统默认值是1,因此">/dev/null"等同于"1>/dev/null"
2 表示stderr标准错误
& 表示等同于的意思,2>&1,表示2的输出重定向等同于1
1 表示stdout标准输出,系统默认值是1,因此">/dev/null"等同于 "1>/dev/null" 

&>/dev/null 等价于 >/dev/null 2>&1 
那么本文标题的语句:
1>/dev/null 首先表示标准输出重定向到空设备文件,也就是不输出任何信息到终端,说白了就是不显示任何信息。
2>&1 接着,标准错误输出重定向等同于标准输出,由于以前标准输出已经重定向到了空设备文件,因此标准错误输出也重定向到空设备文件。

--------------------------------------------------------------
在一些Shell脚本中,特别是Crontab的脚本中,常常会看到 >/dev/null 2>&1这样的写法。
其实这个很好理解。咱们分两部分解释。
1.     >/dev/null
你们知 “>”(右尖括号) 在unix/linux shell 中表示 输入到 的意思,就是把”>”左边的内容输入到”>”右边。
好比:echo text>1.txt 就把“text”这个文本输入到1.txt这个文件中。
那么 “/dev/null” 又是个什么东东呢?它表明一个空设备,即不存在的设备。也就是说,抛弃”>”左边的内容,不进行输出。
2.     2>&1
这个实际上是三个部分组成的:2, >&, 1 。咱们先来搞清楚这里的2和1是什么意思。在/usr/include/unistd.h中,你能够找到以下代码。
/* Standard file descriptors.  */
#define STDIN_FILENO    0   /* Standard input.  */
#define STDOUT_FILENO   1   /* Standard output.  */
#define STDERR_FILENO   2   /* Standard error output.  */
这是三种不一样的流。
2表明stderr.
1表明sdtout.
而 &> 则表示把符号左边的内容以符号右边的形式输出。
2&>1 就是把,把stderr作为stdout输出。
如今咱们结合这两个部分来看。2&>1定义了把stderr作为标准的stdout流输出,而后stdout的内容所有写入/dev/null,也就是说被舍弃掉。
结论就是,不管执行的是什么命令,哪怕运行中出现了error都不会有回显。

-----------------------------------------------------------------
为何要用 /dev/null 2>&1 这样的写法.
这条命令的意思是将标准输出和错误输出所有重定向到/dev/null中,也就是将产生的全部信息丢弃.下面我就为你们来讲一下, command > file 2>file   与command > file 2>&1 有什么不一样的地方.
       首先~command > file 2>file 的意思是将命令所产生的标准输出信息,和错误的输出信息送到file 中.command   > file 2>file 这样的写法,stdout和stderr都直接送到file中, file会被打开两次,这样stdout和stderr会互相覆盖,这样写至关使用了FD1和FD2两个同时去抢占file 的管道.
       而command >file 2>&1 这条命令就将stdout直接送向file, stderr 继承了FD1管道后,再被送往file,此时,file 只被打开了一次,也只使用了一个管道FD1,它包括了stdout和stderr的内容.
       从IO效率上,前一条命令的效率要比后面一条的命令效率要低,因此在编写shell脚本的时候,较多的时候咱们会用command > file 2>&1 这样的写法.

----------------------------------------------------------------
> /dev/null 2>&1

You need to understand the theory first and then its upto you how and where you want to apply that theory. I'll try to explain above to you.

The greater-than (>) in commands like these redirect the program’s output somewhere. In this case, something is being redirected into /dev/null, and something is being redirected into &1.

Standard in, out and error:

There are three standard sources of input and output for a program. Standard input usually comes from the keyboard if it’s an interactive program, or from another program if it’s processing the other program’s output. The program usually prints to standard output, and sometimes prints to standard error. These three file descriptors (you can think of them as “data pipes”) are often called STDIN, STDOUT, and STDERR.

Sometimes they’re not named, they’re numbered! The built-in numberings for them are 0, 1, and 2, in that order. By default, if you don’t name or number one explicitly, you’re talking about STDOUT.

That means file descriptor 0 or fd0 denotes STDIN or standard input and file descriptor 1 or fd1 denotes STDOUT or standard output and file descriptor 2 or fd2 denotes STDERR or standard error.

You can see the command above is redirecting standard output into /dev/null, which is a place you can dump anything you don’t want (often called the bit-bucket), then redirecting standard error into standard output (you have to put an & in front of the destination when you do this).

The short explanation, therefore, is “all output from this command should be shoved into a black hole.” That’s one good way to make a program be really quiet!
相关文章
相关标签/搜索