LX0-103 · Question #15
In Bash, inserting 1>&2 after a command redirects
The correct answer is C. standard output to standard error.. In Bash, the notation N>&M duplicates file descriptor M onto N; since fd 1 is stdout and fd 2 is stderr, 1>&2 redirects standard output to standard error.
Question
In Bash, inserting 1>&2 after a command redirects
Options
- Astandard error to standard input.
- Bstandard input to standard error.
- Cstandard output to standard error.
- Dstandard error to standard output.
- Estandard output to standard input.
How the community answered
(18 responses)- B6% (1)
- C89% (16)
- E6% (1)
Why each option
In Bash, the notation N>&M duplicates file descriptor M onto N; since fd 1 is stdout and fd 2 is stderr, 1>&2 redirects standard output to standard error.
Standard error (fd 2) is not involved as a destination in 1>&2; this redirection only moves stdout (fd 1) to wherever stderr (fd 2) currently points.
Redirecting standard input to standard error would require 0>&2, not 1>&2; the source descriptor here is 1 (stdout), not 0 (stdin).
Bash assigns file descriptor 1 to standard output and file descriptor 2 to standard error. The redirection 1>&2 uses the file descriptor duplication operator >&, causing the kernel to make fd 1 point to the same underlying stream as fd 2. Any data subsequently written to stdout is therefore delivered to the standard error stream, which is useful for ensuring diagnostic output appears even when stdout is redirected elsewhere.
Redirecting standard error to standard output is the opposite operation, expressed as 2>&1, not 1>&2.
Redirecting standard output to standard input would require 1>&0 and is not a valid interpretation of 1>&2.
Concept tested: Bash file descriptor duplication with 1>&2
Source: https://www.gnu.org/software/bash/manual/bash.html#Duplicating-File-Descriptors
Topics
Community Discussion
No community discussion yet for this question.