zoukankan      html  css  js  c++  java
  • Difference between 2>&-, 2>/dev/null, |&, &>/dev/null and >/dev/null 2>&1

    Reference link: http://unix.stackexchange.com/questions/70963/difference-between-2-2-dev-null-dev-null-and-dev-null-21

     

    Just looking for the difference between

    • 2>&-
    • 2>/dev/null
    • |&
    • &>/dev/null
    • >/dev/null 2>&1

    and their portability with non-Bourne shells like tcshmksh, etc.

    shareimprove this question
     
        
    Note that, while mksh supports &> for GNU bash compatibility, it’s strongly encouraged to not use this, as parsing it can break the semantics of existing POSIX scripts, and mksh disables that in POSIX mode already. –  mirabilos Feb 27 at 13:56
    add comment

    2 Answers

    For background:

    • number 1 = standard out (i.e. STDOUT)
    • number 2 = standard error (i.e. STDERR)
    • if a number isn't explicitly given, then number 1 is assumed by the shell (bash)

    First let's tackle the function of these. For reference see the Advanced Bash-Scripting Guide.

    Functions

    2>&-

    The general form of this one is M>&-, where "M" is a file descriptor number. This will close output for whichever file descriptor is referenced, i.e. "M".

    2>/dev/null

    The general form of this one is M>/dev/null, where "M" is a file descriptor number. This will redirect the file descriptor, "M", to /dev/null.

    2>&1

    The general form of this one is M>&N, where "M" & "N" are file descriptor numbers. It combines the output of file descriptors "M" and "N" into a single stream.

    |&

    This is just an abbreviation for 2>&1. It was added in Bash 4.

    &>/dev/null

    This is just an abbreviation for 2>&1 >/dev/null. It too was added in Bash 4. It redirects file descriptor 2 (STDERR) and descriptor 1 (STDOUT) to /dev/null.

    >/dev/null

    This is just an abbreviation for 1>/dev/null. It redirects file descriptor 1 (STDOUT) to /dev/null.

    Portability to non-bash, tcsh, mksh, etc.

    I've not dealt much with other shells outside of csh and tcsh. My experience with those 2 compared to bash's redirection operators, is that bash is superior in that regard. See the tcsh man page for more details.

    Of the commands you asked about none are directly supported by csh/tcsh. You'd have to use different syntaxes to construct similar functions.

  • 相关阅读:
    20150212-2015
    SM30维护视图添加按钮
    SAP保存操作记录CDHDR和CDPOS表
    20150123-慢慢
    20150124-轻轻
    维护 物料主数据 号码段
    ABAP DEMO-2018
    工具
    幽门螺杆菌资料收集
    MySQL 8 连接时出现 1251 和 2059 错误
  • 原文地址:https://www.cnblogs.com/rosepotato/p/3709315.html
Copyright © 2011-2022 走看看