010-100 · Question #24
Which of the following commands redirects the output of cmd to the file foo.txt, in which an existing file is overwritten?
The correct answer is E. cmd > foo.txt. The > operator redirects a command's standard output to a file, overwriting the file if it already exists.
Question
Which of the following commands redirects the output of cmd to the file foo.txt, in which an existing file is overwritten?
Options
- Acmd || foo.txt
- Bcmd | foo.txt
- Ccmd && foo.txt
- Dcmd >> foo.txt
- Ecmd > foo.txt
How the community answered
(16 responses)- A13% (2)
- B6% (1)
- C6% (1)
- E75% (12)
Why each option
The > operator redirects a command's standard output to a file, overwriting the file if it already exists.
|| is the logical OR operator that runs the right-hand command only if the left-hand command fails; it does not redirect output to a file.
| is the pipe operator that connects the standard output of cmd to the standard input of another command; foo.txt is not a command and this would fail.
&& is the logical AND operator that runs the right-hand command only if the left-hand command succeeds; it does not redirect output to a file.
>> appends standard output to the file rather than overwriting it, so existing content is preserved.
The > operator in shell redirects standard output of cmd to foo.txt, truncating and overwriting the file if it exists, or creating it if it does not. This is the standard single-arrow output redirection in POSIX shells.
Concept tested: Shell output redirection to file with overwrite
Source: https://www.gnu.org/software/bash/manual/bash.html#Redirections
Topics
Community Discussion
No community discussion yet for this question.