In Bash, which redirection sends both stdout and stderr to the same file?

Study for the OSCP Linux Exam. Use our flashcards and multiple-choice questions to test your skills. Each query comes with detailed hints and explanations to enhance your preparedness. Get ready to conquer the exam!

Multiple Choice

In Bash, which redirection sends both stdout and stderr to the same file?

Explanation:
Merging standard output and standard error into one file in Bash is about directing both streams to the same destination. The &> redirection is a Bash-specific shorthand that does exactly this: command &> file.txt directs both stdout and stderr to file.txt. This is the cleanest way to ensure every message from a command ends up in the same log. Why this works well: stdout carries the regular output, while stderr carries error messages. Redirecting both to the same file makes a complete record of what happened, without having to separately track outputs and errors. By contrast, redirecting stdout only (command > file.txt) captures just the normal output, leaving errors visible (or redirected somewhere else). Redirecting errors only (command 2> file.txt) sends errors to a separate file. If you’re not using Bash, the portable approach is command > file.txt 2>&1, which first redirects stdout to the file and then makes stderr follow stdout. The input redirection (<) is for reading from a file, not for combining output streams.

Merging standard output and standard error into one file in Bash is about directing both streams to the same destination. The &> redirection is a Bash-specific shorthand that does exactly this: command &> file.txt directs both stdout and stderr to file.txt. This is the cleanest way to ensure every message from a command ends up in the same log.

Why this works well: stdout carries the regular output, while stderr carries error messages. Redirecting both to the same file makes a complete record of what happened, without having to separately track outputs and errors.

By contrast, redirecting stdout only (command > file.txt) captures just the normal output, leaving errors visible (or redirected somewhere else). Redirecting errors only (command 2> file.txt) sends errors to a separate file. If you’re not using Bash, the portable approach is command > file.txt 2>&1, which first redirects stdout to the file and then makes stderr follow stdout. The input redirection (<) is for reading from a file, not for combining output streams.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy