Linux Unix 命令行上的 2> 是什么意思?

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

What does the 2> mean on the Unix command-line?

linuxbashshellunixcommand-line-interface

提问by Smith

scriptlist=`ls $directory_/fallback_* 2> /dev/null`

What exactly is the purpose of the 2>part of the command? I omitted it and ran the command, it just works fine.

2>命令部分的目的究竟是什么?我省略了它并运行了命令,它工作正常。

And, if the output of ls is getting stored in /dev/null file, what exactly the variable scriptlistwill contain. When I executed the code, the output was in the variable and nothing was there in file null. If we remove 2, then output is in file instead of variable. Any idea what exactly this line of code doing?

并且,如果 ls 的输出存储在 /dev/null 文件中,那么该变量scriptlist将包含什么内容。当我执行代码时,输​​出在变量中,而在 file 中没有任何内容null。如果我们删除2,则输出在文件中而不是变量中。知道这行代码到底在做什么吗?

回答by devnull

any idea what exactly the '2' is doing over here

知道这里的“2”到底在做什么

Here 2is a file descriptor referring to STDERR.

2是一个文件描述符,引用STDERR.

2> /dev/nullimplies that STDERRbe redirected to the null device /dev/null.

2> /dev/null意味着STDERR被重定向到空设备/dev/null

The complete line you've mentioned stores the output, i.e. STDOUT(ignoring the STDERR), returned by ls $directory_/fallback_*into the variable scriptlist.

您提到的完整行存储输出,即STDOUT(忽略STDERR),返回ls $directory_/fallback_*到变量scriptlist

回答by Yu Hao

File descriptor 2represents standard error. (other special file descriptors include 0for standard input and 1for standard output).

文件描述符2代表标准错误。(其他特殊文件描述符包括0标准输入和1标准输出)。

2> /dev/nullmeans to redirect standard error to /dev/null. /dev/nullis a special device that discards everything that is written to it.

2> /dev/null意味着将标准错误重定向到/dev/null. /dev/null是一种特殊的设备,它会丢弃写入其中的所有内容。

Putting all together, this line of code stores the standard output of command ls $directory_/fallback_* 2> /dev/nullinto the variable scriptlist, and the standard error is discarded.

综上所述,这行代码将 command 的标准输出存储ls $directory_/fallback_* 2> /dev/null到变量 中scriptlist,并丢弃标准错误。

回答by Suvarna Pattayil

scriptlist=`ls $directory_/fallback_* 2> /dev/null`

As you have enclosed the whole line ls $directory_/fallback_* 2> /dev/nullin backticks, the output of the lscommand is stored in scriptlistvariable.

由于您ls $directory_/fallback_* 2> /dev/null用反引号将整行括起来,因此ls命令的输出存储在scriptlist变量中。

Also, the 2>is for redirectingthe output of stderrto /dev/null(nowhere).

此外,2>用于输出重定向stderr/dev/nullnowhere)。