nerdexam
CompTIA

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.

GNU and Unix Commands

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)
  • B
    94% (30)
  • C
    3% (1)
  • D
    3% (1)

Why each option

The redirection operator 1> sends standard output (file descriptor 1) to a file, overwriting its contents.

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

Appending stdout to a file requires the >> operator (or 1>>), not 1>.

BThe stdout from the command foo overwrites the file bar.Correct

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.

CThe command foo receives its stdin from the file bar.

Redirecting stdin from a file uses the < operator (or 0<), not 1>.

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

Piping stdout of one command to stdin of another uses the pipe operator |, not file redirection.

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

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

#stdout redirection#file descriptor#shell operators#output redirection

Community Discussion

No community discussion yet for this question.

Full LX0-103 Practice