1

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--

sandun dhammika
  • 881
  • 3
  • 12
  • 31

1 Answers1

4

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)

Jo So
  • 25,005
  • 6
  • 42
  • 59