Which of the following shell redirections will write standard output and standard error output to a file named filename?
Click on the arrows to vote for the correct answer
A. B. C. D. E.B
The correct answer is B. >&
is used for redirecting both standard output and standard error to a file.
Explanation of each option:
A. 2>&1 >filename
: This redirects standard error to the same place as standard output, which is usually the terminal, and then redirects standard output to a file named filename. So, only standard output is redirected to the file, while standard error goes to the terminal.
B. >filename 2>&1
: This first redirects standard output to a file named filename, and then redirects standard error to the same place as standard output, which is the file named filename. So, both standard output and standard error are redirected to the file.
C. 1>&2>filename
: This redirects standard output to the same place as standard error, which is usually the terminal, and then redirects standard error to a file named filename. So, only standard error is redirected to the file, while standard output goes to the terminal.
D. >>filename
: This appends standard output to a file named filename, but does not redirect standard error.
E. 1&2>filename
: This is not a valid shell redirection. The correct syntax for redirecting both standard output and standard error is >&
.
Therefore, option B (>filename 2>&1
) is the correct answer as it redirects both standard output and standard error to a file named filename.