What does the shell do when you use > file.txt before a command?

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 does the shell do when you use > file.txt before a command?

Explanation:
Redirecting output with the > operator sends the command’s standard output to a file instead of the screen. Before the command runs, the shell opens file.txt for writing, truncates it if it already exists (or creates it if it doesn’t), and connects the process’s stdout (file descriptor 1) to that file. As a result, any normal output from the command goes into file.txt. If you want to append instead of overwriting, use >>; to redirect errors as well, use 2> for standard error or combine streams with >file 2>&1.

Redirecting output with the > operator sends the command’s standard output to a file instead of the screen. Before the command runs, the shell opens file.txt for writing, truncates it if it already exists (or creates it if it doesn’t), and connects the process’s stdout (file descriptor 1) to that file. As a result, any normal output from the command goes into file.txt. If you want to append instead of overwriting, use >>; to redirect errors as well, use 2> for standard error or combine streams with >file 2>&1.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy