Questão de Linux Administration #019e3987
In Bash, inserting 1>&2 after a command redirects
standard error to standard input.
standard input to standard error.
standard output to standard error.
standard error to standard output.
standard output to standard input.
Explicação Oficial / Explicações da Comunidade
Summary
In Bash, 1>&2 is a redirection operator that redirects file descriptor 1 (standard output) to file descriptor 2 (standard error). This is commonly used to send normal output to the same place as error messages, for example when you want to combine both streams or when you want to ensure that output from a command is treated as an error. The correct answer is C: standard output to standard error.
How it Works
Bash uses file descriptors to manage input/output streams. By default, file descriptor 0 is standard input (stdin), 1 is standard output (stdout), and 2 is standard error (stderr). The syntax M>&N redirects file descriptor M to a copy of file descriptor N. So 1>&2 means "make file descriptor 1 point to whatever file descriptor 2 points to". After this redirection, any output written to stdout will go to the stderr destination (typically the terminal, but can be redirected elsewhere).
Why the correct answer is right
Option C states "standard output to standard error." This is exactly what 1>&2 does: it redirects stdout (1) to stderr (2). After the redirection, both stdout and stderr write to the same place (the stderr stream). This is useful for merging output or for sending normal output to error logs.
Why incorrect options are wrong
- A. standard error to standard input. This would be
2<&0or2>&0? Actually, redirecting stderr to stdin is unusual;2>&0would redirect stderr to stdin, but that's not1>&2. Also, stdin is for input, not output. - B. standard input to standard error. This would be
0>&2, which redirects stdin to stderr. But stdin is an input stream, so redirecting it to an output stream doesn't make sense in typical usage. - D. standard error to standard output. This is
2>&1, which is the opposite of1>&2. It redirects stderr to stdout. - E. standard output to standard input. This would be
1<&0or1>&0? Actually,1>&0would redirect stdout to stdin, but stdin is an input stream, so writing to it is not standard. The correct syntax for redirecting stdout to stdin is not commonly used; typically you use pipes or process substitution.