Reputation: 22244
Please confirm if my understandings are correct. Or correct if wrong and pointers to related technical article and documents would be appreciated.
These are doing the same.
ls &>/tmp/01.log
ls 2>&1 >/tmp/02.log
ls > /tmp/03.log 2>&1
ls 2>&1 1>/tmp/04.log
The result of 5 will be the same with 1 to 4 on a single core CPU where there will be no multiple execution threads in a process. If it could produce a different result, please help understand what is actually happening in each process and in the kernel.
For multi-core CPU environment, would it happen that while a thread on a core is writing to stdout, another thread on another core write to stderr and they could be interleaved?
ls 1>/tmp/05.log 2>/tmp/05.log
Below could cause errors e.g. stderr tries to write but stdout has not output anything yet.
ls 1>/tmp/05.log 2>>/tmp/05.log
The same with 1 to 4.
ls 1<&2 > /tmp/06.log
The result is the same with 1 to 4, although makes no sense of doing it.
ls 2>&1 | cat<&0>&1 > /tmp/07.log
Upvotes: 0
Views: 41
Reputation: 22225
Number 4 is different. Order matters: First FD 2 is redirected where FD 1 currently points to (by default the terminal, unless another redirection had been set up already in this process or by the parent process), and then FD 1 is redirected to the file.
Upvotes: 1