Bird
0
0

The following script is intended to write two lines to output.txt but only writes one line. What is the error?

medium📝 Debug Q14 of 15
Bash Scripting - File Operations in Scripts
The following script is intended to write two lines to output.txt but only writes one line. What is the error?
echo "First line" > output.txt
printf "Second line" > output.txt
Aprintf needs \n to add newline
BSecond command overwrites the file instead of appending
Cecho should use >> to append
DMissing quotes around Second line
Step-by-Step Solution
Solution:
  1. Step 1: Check file redirection operators

    The first command uses > which creates or overwrites output.txt with "First line".
  2. Step 2: Check second command redirection

    The second command also uses > which overwrites output.txt, replacing previous content.
  3. Step 3: Correct approach

    To keep both lines, second command should use >> to append instead of overwrite.
  4. Final Answer:

    Second command overwrites the file instead of appending -> Option B
  5. Quick Check:

    Using > twice overwrites file = D [OK]
Quick Trick: Use >> to append, > overwrites file [OK]
Common Mistakes:
MISTAKES
  • Thinking \n missing causes no second line
  • Confusing > and >> operators
  • Assuming echo always appends

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes