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.
Correct. generate-report > report.txt redirects stdout and replaces the existing contents.
generate-report >> report.txtIncorrect. >> preserves the old contents and appends.
Incorrect. >> preserves the old contents and appends.
generate-report < report.txtIncorrect. < connects report.txt to standard input.
Incorrect. < connects report.txt to standard input.
generate-report 2> report.txtIncorrect. 2> redirects stderr, leaving stdout on the terminal.
Incorrect. 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 / 一時ファイル
- 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.