Possible Duplicate:Order of redirections Apart from the standalone exec >&2 to redirect the current shell's input and output are there any behavioral differences in the following commands:echo -en C\nB\nA\n | sort 2>/dev/stdout >&2echo -en C\nB\nA\n 2>/dev/stdout >&2 | sort2>/dev/stdout >&2 echo -en C\nB\nA\n | sortIf the above commands were equivalent which is the preferred variant?
Where to place a Bash shell redirection for a command?
bash;shell;io redirection;file descriptors
First, in a simple command (basically, the name of a program followed by some arguments), the relative position of arguments and redirections doesn't matter. You can even have redirections before the command name. The following are all equivalent:foo --bar qux >out 2>errfoo >out --bar 2>err>out 2>err foo --bar qux(I won't list all the possibilities.) It's usual to put the redirections at the end, so you might surprise a future reader if you don't do that. However, it's only a matter of style. There is some benefit to putting input redirections before the command and output redirections afterward, particularly in pipelines, because it keeps the reading order identical to the processing order:<input.txt command1 | command2 | command3 >output.txt(contrast command1 <input.txt | command2 , which puts the origin after the first processing step.)In compound commands, you do need to put the redirections at the end; for example, in the following snippets, you can't put the redirection anywhere else:while some_predicate; do some_action; done <in >out{ command1; command2; } <in >outWhen there are multiple redirections, the order is important if they overlap, i.e. if they have file descriptors in common. See Order of redirectionsIn a pipeline, redirections apply to each piped command. In your examples, the redirections apply to (1) the sort command only, (2,3) the echo command only. As explained above, (2) and (3) are equivalent.In (2) and (3), the output from echo ends up at the same place it was before the redirection. If you'd written echo -en C\nB\nA\n >&2 | sort, there would be no output to stdout from the command on the left-hand side of the pipe, so sort would see an empty stdin.You can redirect the input or output of a block of commands by putting them in braces:{ command1 | command2; } 2>error.logI assume your choice of redirections was for demonstration purposes only  2>/dev/stdout >&2 is a complex way of writing 2>&1 (redirect standard error (i.e. fd 2) to wherever standard output (i.e. fd 1) currently goes).