在 shell 中,什么是 "2>&1"意思? [英] In the shell, what does " 2>&1 " mean?

查看:23
本文介绍了在 shell 中,什么是 "2>&1"意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Unix shell 中,如果我想将 stderrstdout 组合到 stdout 流中以进行进一步操作,我可以附加以下内容在我的命令结束时:

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

所以,如果我想在 g++ 的输出上使用 head,我可以这样做:

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.

有人可以分解一下并逐个解释 2>&1 是什么意思吗?

Can someone break this up and explain character by character what 2>&1 means?

推荐答案

文件描述符 1 是标准输出 (stdout).
文件描述符 2 是标准错误 (stderr).

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

这是记住这个结构的一种方法(尽管它并不完全准确):起初,2>1 可能看起来是一种将 stderr 重定向到的好方法标准输出.但是,它实际上会被解释为将 stderr 重定向到名为 1 的文件".& 表示后面和前面的是文件描述符而不是文件名.所以构造变成:2>&1.

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 and precedes is a file descriptor and not a filename. So the construct becomes: 2>&1.

>& 视为重定向合并运算符.

Consider >& as redirect merger operator.

这篇关于在 shell 中,什么是 "2>&1"意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆