Why is `cmd 2>&1 > file` different from `cmd > file 2>&1`?
AdvancedConceptArguments, I/O, here-docs, process substitution and subshells
Answer
Redirections are processed left to right, and 2>&1 copies wherever fd 1 points at that moment.
> file 2>&1: fd 1 → file, then fd 2 → (fd 1 = file). Both go to the file.2>&1 > file: fd 2 → (fd 1 = terminal), then fd 1 → file. Errors still go to the terminal.
The second form is occasionally useful on purpose: out=$(cmd 2>&1 >/dev/null) captures only stderr.
More on Arguments, I/O, here-docs, process substitution and subshells
How does `getopts` work? Show a complete example.IntermediateWhat is the difference between `getopts` and `getopt`?AdvancedExplain file descriptors 0, 1 and 2 and the main redirection operators.BeginnerWhat is a here-document, and what is the difference between `<<EOF`, `<<'EOF'` and `<<-EOF`?BeginnerWhat is a here-string (`<<<`)?IntermediateHow do you send a here-doc to a remote server over SSH, and when should variables expand locally vs remotely?Advanced