正解:B
Explanation
The shell redirection that will write standard output and standard error output to a file named filename is
>filename 2>&1. This command uses the following syntax:
* filename: Redirects standard output (file descriptor 1) to a file named filename. If the file does not exist, it will be created. If the file exists, it will be overwritten.
* 2>&1: Redirects standard error (file descriptor 2) to the same destination as standard output. The & symbol indicates that the following number is a file descriptor, not a filename.
The order of the redirections is important, as they are processed from left to right. If the order was reversed, as in 2>&1 >filename, the command would not work as expected, because it would first redirect standard error to the current standard output (which is the terminal by default), and then redirect standard output to the file, leaving standard error unchanged.
The other commands are incorrect for the following reasons:
* A. 2>&1 >filename: This command will redirect standard error to the current standard output, and then redirect standard output to the file, leaving standard error unchanged.
* C. 1>&2>filename: This command will redirect standard output to the current standard error, and then redirect standard error to the file, leaving standard output unchanged.
* D. >>filename: This command will only redirect standard output to the file in append mode, without affecting standard error.
* E. 1&2>filename: This command is not valid, as it is missing the > symbol before the first file descriptor, and it will produce an error message.
References:
* [LPI Exam 101 Detailed Objectives], Topic 103: GNU and Unix Commands, Objective 103.4: Use streams, pipes and redirects, Weight: 4, Key Knowledge Areas: Redirecting standard input, standard output and standard error.
* How to redirect and append both standard output and standard error to a file with Bash, Topic:
Substituting Text.