Save the stdout of both date and uptime into one status.txt, overwriting it.
Do not repeat a redirection on each command.
Which Bash form is appropriate?
{ LIST; } > FILE groups commands in the current shell and applies one stdout redirection to the entire group.
Detailed explanation
date ; uptime > status.txtIncorrect. The redirection applies only to uptime; date remains on the terminal.
Incorrect. The redirection applies only to uptime; date remains on the terminal.
date > status.txt ; uptimeIncorrect. Only date is redirected.
Incorrect. Only date is redirected.
{ date; uptime; } > status.txtCorrect. The group sends both commands' stdout to status.txt.
Correct. The group sends both commands' stdout to status.txt.
date | uptime > status.txtIncorrect. The pipe feeds date output to uptime rather than saving both outputs as a group.
Incorrect. The pipe feeds date output to uptime rather than saving both outputs as a group.
Try it yourself
An example you can run in a temporary verification environment.
tmp=$(mktemp); { printf 'one\n'; printf 'two\n'; } >"$tmp"; cat "$tmp"; rm -f "$tmp"Expected result
one
twoKey points
- Brace groups run in the current shell
- A final semicolon is required
- The redirection covers the group
Notes
- Environment: Bash 5.x / 一時ファイル
- 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.