Save generate-report standard output to report.txt.
If report.txt exists, truncate it before writing the new output.
Which redirection is appropriate?
> connects standard output to a file and, unless noclobber applies, truncates an existing regular file before writing.
Detailed explanation
generate-report > report.txtCorrect. generate-report > report.txt redirects stdout and replaces the existing contents.
generate-report >> report.txtIncorrect. >> preserves the old contents and appends.
generate-report < report.txtIncorrect. < connects report.txt to standard input.
generate-report 2> report.txtIncorrect. 2> redirects stderr, leaving stdout on the terminal.
Try it yourself
An example you can run in a temporary verification environment.
tmp=$(mktemp); printf old >"$tmp"; printf 'new\n' >"$tmp"; cat "$tmp"; rm -f "$tmp"Expected result
newKey points
- File descriptor 1 is stdout
- > overwrites
- >> appends
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
Ordinary output redirection creates or truncates the destination before the command writes.
Check the side effect
Use a disposable destination and inspect its contents after a second run.