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

最新文章

  1. UWP 禁止Pivot swip 手势
  2. macos开发pgsql数据库
  3. javafx之两种局部界面的呈现方式
  4. [wxWidgets] 1. 安装及"hello world"程序
  5. Xena测试仪的自动化
  6. 高性能Java网络框架 MINA
  7. hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)
  8. JAVA! static什么作用?
  9. android照片墙的实现
  10. java中-静态代码块、构造代码块、构造方法的联系
  11. python网络编程(UDP+广播)
  12. 【NOIP数论内容整理】
  13. golang 中的指针
  14. Oracle 数据库 Only 导出空表的方法
  15. Win7 启动修复
  16. android: ADB错误“more than one device and emulator”
  17. 2019.02.06 bzoj2187: fraction(类欧几里得)
  18. kali删除软件
  19. 【PaddlePaddle系列】CIFAR-10图像分类
  20. TCP/IP协议、HTTP协议

热门文章

  1. 规约模式Specification Pattern
  2. ubuntu登录黑屏“failed to start session”, gdm+kdm+lightdm
  3. Hive使用简介
  4. react使用引入svg的icon;svg图形制作
  5. Hadoop之Sqoop详解
  6. grep怎样匹配tab键
  7. [Unity-2] Unity播放音乐
  8. HTML5 Canvas 绘制库存变化折线 增加超储告罄线
  9. css3的nth-child选择器的具体探讨
  10. python抓包截取http记录日志