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:

  1. 2>&1

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

  1. 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

In the shell, what does “ 2>&1 ” mean?的更多相关文章

  1. Shell替换

    如果表达式中包含特殊字符,Shell 将会进行替换.例如,在双引号中使用变量就是一种替换,转义字符也是一种替换. #!/bin/bash a= echo -e "Value of a is ...

  2. Shell特殊变量

    $ 表示当前Shell进程的ID,即pid $echo $$ 运行结果 特殊变量列表 变量 含义 $0 当前脚本的文件名 $n 传递给脚本或函数的参数.n 是一个数字,表示第几个参数.例如,第一个参数 ...

  3. shell变量

    定义变量 定义变量时,变量名不加美元符号($),如: variableName="value" 注意,变量名和等号之间不能有空格,这可能和你熟悉的所有编程语言都不一样.同时,变量名 ...

  4. 第一个shell脚本

    打开文本编辑器,新建一个文件,扩展名为sh(sh代表shell),扩展名并不影响脚本执行,见名知意就好. #!/bin/bash echo "Hello World !" &quo ...

  5. shell简介

    Shell作为命令语言,它交互式地解释和执行用户输入的命令:作为程序设计语言,它定义了各种变量和参数,并提供了许多在高级语言中才具有的控制结构,包括循环和分支. shell使用的熟练程度反映了用户对U ...

  6. Shell碎碎念

    1. 字符串如何大小写转换 str="This is a Bash Shell script." 1> tr方式 newstr=`tr '[A-Z]' '[a-z]' < ...

  7. MongoDB学习笔记二—Shell操作

    数据类型 MongoDB在保留JSON基本键/值对特性的基础上,添加了其他一些数据类型. null null用于表示空值或者不存在的字段:{“x”:null} 布尔型 布尔类型有两个值true和fal ...

  8. 使用C#给Linux写Shell脚本

    在这个逼格决定人格,鄙视链盛行的年头,尤其是咱们IT界,请问您今天鄙视与被鄙视的次数分别是多少?如果手中没有一点压箱的本事,那就只有看的份了.今天我们也要提升下自己的格调,学习些脑洞大开的东西,学完之 ...

  9. Linux环境下shell和vim中乱码原因及消除办法

    shell和vim中乱码原因及消除办法 作者:Jack47 在Linux下开发,经常遇到乱码问题:shell或者vim中显示不了中文,或者能够显示,但不能输入中文.每次都是上网去搜,或者同事告诉我一些 ...

  10. 【说解】在shell中通过mkfifo创建命名管道来控制多个进程并发执行

    背景: 工作中有两个异地机房需要传数据,数据全名很规范,在某个目录下命名为统一的前缀加上编号.如/path/from/file.{1..100}.而机房间的专线对单个scp进程的传输速度是有限制的,比 ...

随机推荐

  1. java只有值传递,不存在引用传递

    今天,我在一本面试书上看到了关于java的一个参数传递的问题: 写道 java中对象作为参数传递给一个方法,到底是值传递,还是引用传递? 我毫无疑问的回答:“引用传递!”,并且还觉得自己对java的这 ...

  2. Oracle JET 起步

    Oracle JET介绍 Oracle JET是一个用于经验丰富的JavaScript开发人员构建客户端的基于JavaScript的应用程序的工具包.(抱歉本人对JavaScript绝对是从零开始) ...

  3. Thunderbird使用发邮件模板

    Thunderbird的强大之处是可以使用多种第三方插件,其中有个插件SmartTemplate4,是用来设置Thunderbird发件模板的. 然后,模板设置内容如下: <p>:< ...

  4. Python 的 LEGB 规则(转载)

    转载:https://mp.weixin.qq.com/s?timestamp=1498528588&src=3&ver=1&signature=DfFeOFPXy44ObCM ...

  5. ant Form 常用 api

    经过 Form.create 包装的组件将会自带 this.props.form 属性,this.props.form 提供的 API 如下: 注意:使用 getFieldsValue getFiel ...

  6. iOSeasy造成循引用的场景

    场景一 :NStimer timer就是一个能在从如今開始的未来的某一个时刻又或者周期性的运行我们指定的方法的对象 NSTimer运行的必要条件:相应线程的RunLoop要开启,mode要相应 以下看 ...

  7. WP8控件进度条(ProgressBar)的使用

    --前台代码 <Grid x:Name="ContentPanel"  Grid.Row="1" Margin="0,0,24,0"& ...

  8. python 列表,元组,字符串方法和属性

    python序列包含列表[].元组().字符串三种 -------列表-------------- 一.列表基本内容 1.建立:a=[1,2,3,5],通过[ , ,], >>>b= ...

  9. 【MyBatis学习02】走进MyBatis的世界

    mybatis是个持久层的框架,用来执行数据库操作的,无外乎增删改查,上一节对mybatis有了宏观上的了解后,这一篇博客主要通过一个小示例来入门mybatis,先看一下要写的示例需求: 根据用户id ...

  10. ASP.NET_SessionId 不相同导致验证码出错

    问题: 今天碰到一个比较奇怪的问题,每个页面请求的cookie的ASP.NET_SessionId 不相同,导致验证码检验出错.也就是说每个请求都是一个新的会话,我们把验证码保证在Session[ra ...