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.
date > status.txt ; uptimeIncorrect. Only date is redirected.
{ date; uptime; } > status.txtCorrect. 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.
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 / 一時ファイル
- Use temporary files and harmless commands for stream experiments.
- Shell redirection syntax and xargs behavior should be checked on the target shell implementation.
Foundation review
Place the operation
Grouping determines which commands share the redirection and whether shell state is isolated.
Check the side effect
Use harmless output commands and verify that every line reaches the destination.