Questão de Linux Administration #019e3987
Which of the following statements is correct regarding the command foo 1> bar?
The stdout from the command foo is appended to the file bar.
The stdout from the command foo overwrites the file bar.
The command foo receives its stdin from the file bar.
The command foo receives its stdin from the stdout of the command bar.
The stderr from the command foo is saved to the file bar.
Explicação Oficial / Explicações da Comunidade
The command foo 1> bar redirects the standard output (stdout) of the command foo to the file bar, overwriting the file if it exists. The number 1 represents the file descriptor for stdout, and the > operator performs output redirection with truncation (overwrite).
How it Works
In Unix/Linux shells, each process has three standard file descriptors: stdin (0), stdout (1), and stderr (2). The > operator redirects stdout to a file, creating or overwriting it. The syntax 1> is explicit, but > alone defaults to stdout. To append instead of overwrite, use >>.
Why the Correct Value is Right
The statement "The stdout from the command foo overwrites the file bar" is correct because 1> truncates the target file before writing. This is the default behavior of single > redirection.
Why the Incorrect Values are Wrong
- "The stdout from the command foo is appended to the file bar" is incorrect because appending requires
>>(double greater-than), not>. - "The command foo receives its stdin from the file bar" is incorrect because stdin redirection uses
<(e.g.,foo < bar), not>. - "The command foo receives its stdin from the stdout of the command bar" is incorrect because that describes a pipe (
foo | bar), not file redirection. - "The stderr from the command foo is saved to the file bar" is incorrect because stderr is file descriptor 2; to redirect stderr, use
2>(e.g.,foo 2> bar).