Linux Administration
medium

Pregunta de Linux Administration #019e3987

Enunciado de la Pregunta
Linux Administration
#stdout-redirection
#file-descriptor
#overwrite
#shell-redirection
#1>
Medio

Which of the following statements is correct regarding the command foo 1> bar?

A

The stdout from the command foo is appended to the file bar.

B

The stdout from the command foo overwrites the file bar.

Correcto
C

The command foo receives its stdin from the file bar.

D

The command foo receives its stdin from the stdout of the command bar.

E

The stderr from the command foo is saved to the file bar.

Explicación Oficial / Explicaciones de la Comunidad

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).