Currently I\'m coding a small shell (redirection, pipes, exec, etc.). Been trying to figure out the steps the Linux shell takes in addressing I/O redirection.
Some q
As long as you only redirect stdin and stdout, the order in which the redirections are processed don't matter so the last two examples are exactly the same.
BASH processes IO redirection from left to right.
> cmd1 > file 2>&1
> cmd2 2>&1 > file
These two are different. In the first case, I bind stdout to file
and then bind stderr to stdout: Both stderr and stdout now go into the file.
In the second case, I bind (the child's) stderr to stdout (of the parent) and then I find the child's stdout to file. The result is that you now get the child's stderr output on stdout and the stdout goes to the file. This is useful for processing stderr in a pipe, for example.
If you look at the source code of BASH, you can see that the execution of a command is split in several steps: