bash 如何将所有输出重定向到 /dev/null?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18012930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:00:02  来源:igfitidea点击:

How can I redirect all output to /dev/null?

bashpipeoutput-redirectdev-null

提问by Benubird

I want to run a program (google-chrome) in the background, but prevent it from outputting any messages to the terminal.

我想google-chrome在后台运行一个程序 ( ),但阻止它向终端输出任何消息。

I tried doing this:

我尝试这样做:

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

However, the terminal still fills up without messages like:

但是,终端仍然填满而没有如下消息:

[5746:5746:0802/100534:ERROR:object_proxy.cc(532)] Failed to call method: org.chromium.Mtpd.EnumerateStorag...

[5746:5746:0802/100534:ERROR:object_proxy.cc(532)] 调用方法失败:org.chromium.Mtpd.EnumerateStorag...

What am I doing wrong? How do I redirect allthe output to /dev/null?

我究竟做错了什么?如何将所有输出重定向到/dev/null

回答by Michael Martinez

Redirection operators are evaluated left-to-right. You wrongly put 2>&1first, which points 2to the same place, as 1currently is pointed to which is the local terminal screen, because you have not redirected 1yet. You need to do either of the following:

重定向运算符从左到右计算。你错误地把2>&1指向2同一个地方的第一个指向了1当前指向的本地终端屏幕,因为你还没有重定向1。您需要执行以下任一操作:

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

Or

或者

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

The placement of the redirect operators in relation to the command does not matter. You can put them before or after the command.

与命令相关的重定向运算符的位置无关紧要。您可以将它们放在命令之前或之后。

回答by user1146332

In the section Redirection, Bash's reference manual says:

Redirection部分,Bash 的参考手册说:

The operator [n]>&wordis used [...] to duplicate output file descriptors

该运算符[n]>&word用于 [...] 复制输出文件描述符

To redirect both standard error and standard output to fileyou should use the form

要将标准错误和标准输出重定向到file您应该使用表单

&>file

With regard to your case, that means substitute

关于你的情况,这意味着替代

2>&1 1>/dev/null

with

&>/dev/null

回答by user1146332

It seems that syntax is different:

似乎语法不同:

./a.out 1>/dev/null 2>&1 &

See the devices for FD = 2 are different when ./a.out 1>/dev/null 2>&1and ./a.out 2>&1 1>/dev/null &

看到 FD = 2 的设备在./a.out 1>/dev/null 2>&1./a.out 2>&1 1>/dev/null &

1) FD=2 points to /dev/null

1) FD=2 指向 /dev/null

>./a.out 1>/dev/null 2>&1 &
[1] 21181
>lsof -p `pidof a.out`
COMMAND   PID            USER   FD   TYPE DEVICE SIZE/OFF      NODE NAME
a.out   21181 xxxxxxxxxxxxxxx    0u   CHR 136,43      0t0        46 /dev/pts/43
a.out   21181 xxxxxxxxxxxxxxx    1w   CHR    1,3      0t0      3685 /dev/null
a.out   21181 xxxxxxxxxxxxxxx    2w   CHR    1,3      0t0      3685 /dev/null

2) FD=2 points to /dev/pts/43

2) FD=2 指向 /dev/pts/43

>./a.out 2>&1 1>/dev/null &
[1] 25955
>lsof -p `pidof a.out`
COMMAND   PID            USER   FD   TYPE DEVICE SIZE/OFF      NODE NAME
a.out   25955 xxxxxxxxxxxxxxx    0u   CHR 136,43      0t0        46 /dev/pts/43
a.out   25955 xxxxxxxxxxxxxxx    1w   CHR    1,3      0t0      3685 /dev/null
a.out   25955 xxxxxxxxxxxxxxx    2u   CHR 136,43      0t0        46 /dev/pts/43