LX0-103 · Question #126
Which of the following statements is correct regarding the command foo 1> bar?
The correct answer is B. The stdout from the command foo overwrites the file bar.. The redirection operator 1> sends standard output (file descriptor 1) to a file, overwriting its contents.
Question
Which of the following statements is correct regarding the command foo 1> bar?
Options
- AThe stdout from the command foo is appended to the file bar.
- BThe stdout from the command foo overwrites the file bar.
- CThe command foo receives its stdin from the file bar.
- DThe command foo receives its stdin from the stdout of the command bar.
- EThe stderr from the command foo is saved to the file bar.
How the community answered
(32 responses)- B94% (30)
- C3% (1)
- D3% (1)
Why each option
The redirection operator 1> sends standard output (file descriptor 1) to a file, overwriting its contents.
Appending stdout to a file requires the >> operator (or 1>>), not 1>.
In Linux shell syntax, 1> explicitly redirects file descriptor 1 (stdout) to the named file. If the file already exists, it is truncated and overwritten; if it does not exist, it is created. This is functionally equivalent to using > alone, since stdout is the default target for output redirection.
Redirecting stdin from a file uses the < operator (or 0<), not 1>.
Piping stdout of one command to stdin of another uses the pipe operator |, not file redirection.
Redirecting stderr requires file descriptor 2, written as 2>, not 1>.
Concept tested: Shell I/O redirection with file descriptors
Source: https://www.gnu.org/software/bash/manual/bash.html#Redirecting-Output
Topics
Community Discussion
No community discussion yet for this question.