Save both run-check standard output and standard error to all.log, replacing the file.
Use POSIX-style file-descriptor duplication.
Which order is appropriate?
Redirections are processed left to right. Redirect stdout first, then duplicate that file connection onto stderr with 2>&1.
Detailed explanation
run-check > all.log 2>&1Correct. stdout is opened on all.log first, then stderr is duplicated to the same destination.
Correct. stdout is opened on all.log first, then stderr is duplicated to the same destination.
run-check 2>&1 > all.logIncorrect. stderr is copied to the original stdout before stdout is redirected, so it stays on the terminal.
Incorrect. stderr is copied to the original stdout before stdout is redirected, so it stays on the terminal.
run-check > all.log 1>&2Incorrect. The final 1>&2 changes stdout to stderr rather than retaining both on the file.
Incorrect. The final 1>&2 changes stdout to stderr rather than retaining both on the file.
run-check 2> all.log > /dev/nullIncorrect. stdout is sent to /dev/null and is not saved in all.log.
Incorrect. stdout is sent to /dev/null and is not saved in all.log.
Try it yourself
An example you can run in a temporary verification environment.
tmp=$(mktemp); sh -c 'echo out; echo err >&2' >"$tmp" 2>&1; sort "$tmp"; rm -f "$tmp"Expected result
err
outKey points
- Redirections are left to right
- 2>&1 duplicates fd 1
- Both streams share the file
Notes
- Environment: POSIX shell / 一時ファイル
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.