Bird
0
0

What will be the output of the following command if file.txt does not exist?

medium📝 Command Output Q13 of 15
Bash Scripting - File Operations in Scripts
What will be the output of the following command if file.txt does not exist?

cat file.txt > output.txt 2>&1
ARedirects both output and error messages to output.txt
BCreates an empty output.txt file with no error message
COutputs the error message to the terminal and creates output.txt
DThrows a syntax error and does nothing
Step-by-Step Solution
Solution:
  1. Step 1: Understand redirection 2>&1

    This redirects standard error (2) to the same place as standard output (1).
  2. Step 2: Analyze command behavior when file missing

    cat tries to read file.txt, fails, and sends error to stderr, which is redirected to output.txt along with stdout.
  3. Final Answer:

    Redirects both output and error messages to output.txt -> Option A
  4. Quick Check:

    2>&1 merges error into output [OK]
Quick Trick: 2>&1 sends errors to the same place as output [OK]
Common Mistakes:
MISTAKES
  • Thinking error shows on terminal
  • Assuming output.txt stays empty
  • Confusing 2>&1 with separate redirections

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes