问题
could somebody explain the difference between these two codes?
bad_command 2>& >> file.out
and
bad_command >> file.out 2>&
The manual said that these two codes are different,and first command will output nothing to file.out.
So , here are my questions.
1/ what is the reason for that?
2/ Is there is a document which describes how operator precedence works in shell? how shell parses and made it's syntax tree.
3/ What is the correct syntax and order of it?
--Thanks in advance--
回答1:
Both are syntactically wrong. I assume you meant
bad_command 2>&1 >> file.out
and
bad_command >> file.out 2>&1
instead.
Between these, there is a difference. Redirections are imperative statements, which are worked through from left to right. A redirection operates on a process' file descriptors (fds). You might have heard of the standard filedescriptors #0 (standard in/stdin), #1 (standard out/stdout), #2 (standard error/stderr).
The first commandline's redirections read: "Make fd 2 a copy of fd 1, but then change fd 1 to append to 'file.out'" (the second redirection has no effect to fd 2, which still is a copy of what fd 1 was at the beginning)
The seconds ones read: "Change fd 1 to append to 'file.out', and then make fd 2 a copy of fd 1" (the first redirection has effect to the second redirection, bot fds are now the same)
来源:https://stackoverflow.com/questions/12117375/shell-scripting-io-redirecting-precedence-of-operators