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

老子叫甜甜 提交于 2019-11-27 11:47:55

问题


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.

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?


回答1:


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

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.

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.




回答2:


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.

Also, the 2> is for redirecting the output of stderr to /dev/null (nowhere).




回答3:


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

Here 2 is a file descriptor referring to STDERR.

2> /dev/null implies that STDERR be redirected to the null device /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.



来源:https://stackoverflow.com/questions/19108895/what-does-the-2-mean-on-the-unix-command-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!