Save generate-report's standard output to report.txt.
If report.txt exists, replace its previous contents.
Which command is correct?
The > redirection sends standard output to a file and truncates an existing file before the command runs. Use >> for appending.
Detailed explanation
generate-report > report.txtCorrect. > sends standard output to report.txt and replaces its previous contents.
Correct. > sends standard output to report.txt and replaces its previous contents.
generate-report >> report.txtIncorrect. >> appends and preserves the previous contents.
Incorrect. >> appends and preserves the previous contents.
generate-report < report.txtIncorrect. < redirects a file into standard input rather than saving output.
Incorrect. < redirects a file into standard input rather than saving output.
generate-report 2> report.txtIncorrect. 2> redirects standard error, not standard output.
Incorrect. 2> redirects standard error, not standard output.
Try it yourself
An example you can run in a temporary verification environment.
printf old > /tmp/kp-report.txt; printf new > /tmp/kp-report.txt; cat /tmp/kp-report.txt; rm /tmp/kp-report.txtExpected result
newKey points
- > overwrites standard output
- >> appends
- The shell prepares the redirection
Notes
- Environment: Bash 5.2
- 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.