LFCS · Question #726
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 command foo 1> bar redirects the standard output of the foo command to the file bar, overwriting any existing content in bar.
Question
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
(25 responses)- B92% (23)
- C4% (1)
- D4% (1)
Why each option
The command `foo 1> bar` redirects the standard output of the `foo` command to the file `bar`, overwriting any existing content in `bar`.
Appending standard output to a file is achieved using the `>>` operator (e.g., `foo 1>> bar`).
The `1>` operator explicitly redirects file descriptor 1 (standard output). A single `>` redirection operator overwrites the contents of the target file if it already exists, or creates the file if it doesn't.
Receiving standard input from a file is done using the `<` operator (e.g., `foo < bar`).
Receiving standard input from the standard output of another command typically uses a pipe (`|`) (e.g., `bar | foo`).
Redirecting standard error (file descriptor 2) to a file uses the `2>` operator (e.g., `foo 2> bar`).
Concept tested: Standard output redirection
Source: https://www.gnu.org/software/bash/manual/bash.html#Redirections
Topics
Community Discussion
No community discussion yet for this question.