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
What does the 2> mean on the Unix command-line?
提问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 scriptlist
will 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 2
is a file descriptor referring to STDERR
.
这2
是一个文件描述符,引用STDERR
.
2> /dev/null
implies that STDERR
be 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 2
represents standard error. (other special file descriptors include 0
for standard input and 1
for standard output).
文件描述符2
代表标准错误。(其他特殊文件描述符包括0
标准输入和1
标准输出)。
2> /dev/null
means to redirect standard error to /dev/null
. /dev/null
is 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/null
into 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/null
in backticks,
the output of the ls
command is stored in scriptlist
variable.
由于您ls $directory_/fallback_* 2> /dev/null
用反引号将整行括起来,因此ls
命令的输出存储在scriptlist
变量中。
Also, the 2>
is for redirectingthe output of stderr
to /dev/null
(nowhere).