0

Question

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.

  1. ls &>/tmp/01.log

  2. ls 2>&1 >/tmp/02.log

  3. ls > /tmp/03.log 2>&1

  4. 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?

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

  1. ls 1<&2 > /tmp/06.log

The result is the same with 1 to 4, although makes no sense of doing it.

  1. ls 2>&1 | cat<&0>&1 > /tmp/07.log

Researches

1 Answer 1

1

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.