What is the effect of > file.txt 2>&1?

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

What is the effect of > file.txt 2>&1?

Explanation:
This tests how shell I/O redirection and file descriptor duplication work. First, redirecting stdout to a file with > file.txt opens file.txt for writing and truncates it, so any subsequent standard output goes into that file. The part 2>&1 redirects the standard error (descriptor 2) to wherever standard output (descriptor 1) is currently going. Since stdout was just redirected to file.txt, stderr is now sent to the same destination—the file. The result is that both stdout and stderr end up in file.txt. The order matters: if you did 2>&1 > file.txt, stderr would still go to the screen because the stdout redirection wouldn’t affect it after the fact. In short, this command directs both streams to file.txt.

This tests how shell I/O redirection and file descriptor duplication work. First, redirecting stdout to a file with > file.txt opens file.txt for writing and truncates it, so any subsequent standard output goes into that file. The part 2>&1 redirects the standard error (descriptor 2) to wherever standard output (descriptor 1) is currently going. Since stdout was just redirected to file.txt, stderr is now sent to the same destination—the file. The result is that both stdout and stderr end up in file.txt. The order matters: if you did 2>&1 > file.txt, stderr would still go to the screen because the stdout redirection wouldn’t affect it after the fact. In short, this command directs both streams to file.txt.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy