In the shell, what does “ 2>&1 ” mean?
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 (seenoclobber
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
In the shell, what does “ 2>&1 ” mean?的更多相关文章
- Shell替换
如果表达式中包含特殊字符,Shell 将会进行替换.例如,在双引号中使用变量就是一种替换,转义字符也是一种替换. #!/bin/bash a= echo -e "Value of a is ...
- Shell特殊变量
$ 表示当前Shell进程的ID,即pid $echo $$ 运行结果 特殊变量列表 变量 含义 $0 当前脚本的文件名 $n 传递给脚本或函数的参数.n 是一个数字,表示第几个参数.例如,第一个参数 ...
- shell变量
定义变量 定义变量时,变量名不加美元符号($),如: variableName="value" 注意,变量名和等号之间不能有空格,这可能和你熟悉的所有编程语言都不一样.同时,变量名 ...
- 第一个shell脚本
打开文本编辑器,新建一个文件,扩展名为sh(sh代表shell),扩展名并不影响脚本执行,见名知意就好. #!/bin/bash echo "Hello World !" &quo ...
- shell简介
Shell作为命令语言,它交互式地解释和执行用户输入的命令:作为程序设计语言,它定义了各种变量和参数,并提供了许多在高级语言中才具有的控制结构,包括循环和分支. shell使用的熟练程度反映了用户对U ...
- Shell碎碎念
1. 字符串如何大小写转换 str="This is a Bash Shell script." 1> tr方式 newstr=`tr '[A-Z]' '[a-z]' < ...
- MongoDB学习笔记二—Shell操作
数据类型 MongoDB在保留JSON基本键/值对特性的基础上,添加了其他一些数据类型. null null用于表示空值或者不存在的字段:{“x”:null} 布尔型 布尔类型有两个值true和fal ...
- 使用C#给Linux写Shell脚本
在这个逼格决定人格,鄙视链盛行的年头,尤其是咱们IT界,请问您今天鄙视与被鄙视的次数分别是多少?如果手中没有一点压箱的本事,那就只有看的份了.今天我们也要提升下自己的格调,学习些脑洞大开的东西,学完之 ...
- Linux环境下shell和vim中乱码原因及消除办法
shell和vim中乱码原因及消除办法 作者:Jack47 在Linux下开发,经常遇到乱码问题:shell或者vim中显示不了中文,或者能够显示,但不能输入中文.每次都是上网去搜,或者同事告诉我一些 ...
- 【说解】在shell中通过mkfifo创建命名管道来控制多个进程并发执行
背景: 工作中有两个异地机房需要传数据,数据全名很规范,在某个目录下命名为统一的前缀加上编号.如/path/from/file.{1..100}.而机房间的专线对单个scp进程的传输速度是有限制的,比 ...
随机推荐
- html里嵌入CSS的三种方式
在HTML中定义CSS的方式有:Embedding(嵌入式).Linking(引用式).Inline(内联式),下面通过实例为大家详细介绍下它们的特点 在HTML中常用以下3种方式定义CSS:Em ...
- 一个简单的假vue全家桶(vue+vue-router+require)
首先说明我觉得这是一个比较好理解的vue全家桶(虽然是假的),模块化也是用require来做的,而且如果后期有必要压缩我也会用gulp来做 1.依赖个个本地模块,require只是用来载入page,这 ...
- uCOS-ii笔记
ucos ii system 文件结构 上层: 应用软件,用户代码 中层: 与处理器无关代码 与应用程序相关配置文件 与处理器有关代码 下层: 硬件(cpu,interupt,timer,gpio,i ...
- cpp面向对象编程
如下图,先建好文件, 这里用的是Visual studio 2010 当然也可以用eclipse for cpp,如下图: AbstractShape.h #ifndef ABSTRACTSHAPE_ ...
- 用coffeescript实现类java的Map类
class Map constructor : -> @entry = {} @count = 0 size : -> return @count isEmpty : -> retu ...
- 使用webpack构建本地服务器
想不想让你的浏览器监听你的代码的修改,并自动刷新显示修改后的结果,其实Webpack提供一个可选的本地开发服务器,这个本地服务器基于node.js构建,可以实现你想要的这些功能,不过它是一个单独的组件 ...
- vmware已经全面支持open-vm-tools
以后不用再为vmware vm单独安装vmware-tools了,vmware已经全面支持open-vm-tools, 今天突然发现安装vmware-tools时出现deprecated提示,原来vm ...
- 向Solr数据集提交Json格式数据(Scala,Post)
import scalaj.http.Http class SolrAdd () {// 方法接受两个参数,dataType为数据集名称,jsonString为数据json字符串 def postTo ...
- python对solr进行查询、插入操作(GET\POST)
循环查询一个列表中词的个数: #coding=utf-8 import requests ls = [u'我', u'是', u'一个', u'天才', u'你', u'也是'] for item i ...
- Mac上安装使用Nginx
1.brew search nginx 2.brew install nginx 启动nginx ,sudo nginx ;访问localhost:8080 发现已出现nginx的欢迎页面了. 备注: ...