zoukankan      html  css  js  c++  java
  • 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 (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

  • 相关阅读:
    贮油点问题(C++)
    二维数组操作
    二的幂次方(递归)
    [haoi2009]巧克力
    距离最远的牛
    ssh注解basedao简单的实现
    @service中构造方法报错
    spring注解 构造函数问题
    json与gson
    AsyncTask异步类的简单操作
  • 原文地址:https://www.cnblogs.com/pinganzi/p/5291358.html
Copyright © 2011-2022 走看看