Linux 命令末尾的“< /dev/null >& /dev/null”有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8208033/
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-08-06 02:24:03  来源:igfitidea点击:

What does "< /dev/null >& /dev/null" at the end of a command do?

linuxbashstdio

提问by user215997

One of the scripts I run over ssh was hanging and I found a solution for it on this site: http://www.snailbook.com/faq/background-jobs.auto.html

我在 ssh 上运行的一个脚本挂了,我在这个网站上找到了一个解决方案:http: //www.snailbook.com/faq/background-jobs.auto.html

The site resolves the problem by adding this to the end of the command:

该站点通过将其添加到命令末尾来解决该问题:

xterm < /dev/null >& /dev/null &

I think I know what part of it does, but can someone help explain?

我想我知道它的哪一部分,但有人可以帮忙解释一下吗?

The first part:

第一部分:

# For stdin, read from /dev/null
< /dev/null

The second part:

第二部分:

>& /dev/null

What does >&do? I've seen 2>&1which is direct STDERR to STDOUT, but when there are no numbers, does that mean redirect everything to /dev/null?

有什么作用>&?我已经看到2>&1哪个是直接 STDERR 到 STDOUT,但是当没有数字时,这是否意味着将所有内容重定向到/dev/null

回答by Michael Hoffman

Yes, this means redirect both stdout and stderr to /dev/null.

是的,这意味着将 stdout 和 stderr 都重定向到 /dev/null。

From info "(bash)Redirections":

来自info "(bash)Redirections"

Redirecting Standard Output and Standard Error

Bash allows both the standard output (file descriptor 1) and the standard error output (file descriptor 2) to be redirected to the file whose name is the expansion of WORD with this construct.

There are two formats for redirecting standard output and standard error:

&>WORD

and

>&WORD

Of the two forms, the first is preferred. This is semantically equivalent to

>WORD 2>&1

重定向标准输出和标准错误

Bash 允许将标准输出(文件描述符 1)和标准错误输出(文件描述符 2)重定向到名称为具有此结构的 WORD 扩展的文件。

重定向标准输出和标准错误有两种格式:

&>WORD

>&WORD

在这两种形式中,首选第一种。这在语义上等同于

>WORD 2>&1

回答by Basile Starynkevitch

With sshanother way (to achieve what you probably want) is to pass the -fflag, i.e.

有了ssh另一种方式(实现你可能想要的东西)是传递-f标志,即

ssh -X -f your.host.org xterm

回答by Thomas

Googling for 'stream redirection >& ' will let you find http://tldp.org/LDP/abs/html/io-redirection.html

谷歌搜索“流重定向>&”会让你找到 http://tldp.org/LDP/abs/html/io-redirection.html

which, among other things, tells you

其中,除其他外,它告诉你

  >&j

  # Redirects, by default, file descriptor 1 (stdout) to j.
  # All stdout gets sent to file pointed to by j.

Maybe this already helps.

也许这已经有所帮助。

Or search the help of standard unix shells, like bash, for stream redirection.

或者在标准 unix shell 的帮助下搜索流重定向,比如 bash。

Rgds,

Rgds,

Thomas

托马斯