Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
likeunix
V2EX  ›  Linux

重定向的问题

  •  
  •   likeunix · Oct 11, 2016 · 2246 views
    This topic created in 3578 days ago, the information mentioned may be changed or developed.
    cmd >file 2>&1
    cmd 2>&1 >file
    这两条指令的执行结果为什么不一样,命令的细节是什么?
    lxf1992521
        1
    lxf1992521  
       Oct 11, 2016
    等同于:

    cmd 1>file 2>&1
    cmd 2>&1 1>file
    KDr2
        2
    KDr2  
       Oct 12, 2016   ❤️ 1
    - cmd
    stdout -> 终端
    stderr -> 终端

    - cmd >file 2>&1
    先把 stdout(就是 1) dup 到打开的 file 上,这时 stdout(1) 指向 file: stdout -> file
    然后把 stderr(2) dup 到 1 上,因为 1 已经是 file ,所以两个都去了 file : stderr -> stdout 即 stderr -> file


    - cmd 2>&1 >file
    先把 stderr(2) dup 到 stdout 上, stderr -> stdout 即 stderr-> 终端
    再把 stdout(1) dup 到 file 上, stdout-> file
    结果是, stderr 去了原来的 stdout (终端), stdout 去了 file 里
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4161 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 10:12 · PVG 18:12 · LAX 03:12 · JFK 06:12
    ♥ Do have faith in what you're doing.