您的位置:首页 > 运维架构 > Shell

In the shell, what does “ 2>&1 ” mean?

2016-03-18 11:27 483 查看
In a Unix shell, if I want to combine
stderr
and
stdout
into the
stdout
stream for further manipulation, I can append the following on the end of my command:

2>&1

So, if I want to use "head" on the output from g++, I can do something like this:

g++ lots_of_errors 2>&1 | head

so I can see only the first few errors.

I always have trouble remembering this, and I constantly have to go look it up, and it is mainly because I don't fully understand the syntax of this particular trick. Can someone break this up and explain character by character what "2>&1" means?

Zero is
stdin


One is
stdout


Two is
stderr


File descriptor 1 is the standard output (stdout).
File descriptor 2 is the standard error (stderr).

Here is one way to remember this construct (although it is not entirely accurate): at first,
2>1
may look like a good way to redirect stderr to stdout. However, it will actually be interpreted as "redirect stderr to a file named
1
".
&
indicates that what follows is a file descriptor and not a filename. So the construct becomes:
2>&1
.

Symbole
>
mean redirection.

>
mean send to as a whole completed file, overwriting target if exist (see
noclobber
bash feature at #3 later).

>>
mean send in addition to would append to target if exist.

转自: http://stackoverflow.com/questions/818255/in-the-shell-what-does-21-mean
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: