shell 交换stderr stdout 3>&1 1>&2 2>&3

来源:互联网 发布:linux if语句使用方法 编辑:程序博客网 时间:2024/05/21 22:39

"command n>file" redirects command's file descriptor n into the given file. The number before the > is the file descriptor bring modified, and what's after the > is what it's being set to write to.

"&m" in place of a file name says to use the current file descriptor m as the destination.

So it's:

  • Set file descriptor 3 to a copy of file descriptor 1 (so writing to fd 3 also writes to stdout)
  • Set file descriptor 1 to a copy of fd 2 (so writing to fd 1 now writes to stderr)
  • Set file descriptor 2 to a copy of fd 3 (so writing to fd 2 now writes to what used to be stdout)

This leaves fd 3 hanging around. Slightly cleaner would be to make that last one "2>&3-", which would close fd 3 as the last step. But it's basically harmless.



参考:

https://www.reddit.com/r/commandline/comments/1gjq6s/bashlinuxconfusion_about_31_12_23_in_script/

原创粉丝点击