B. Second command overwrites the file instead of appending
C. echo should use >> to append
D. Missing quotes around Second line
Solution
Step 1: Check file redirection operators
The first command uses > which creates or overwrites output.txt with "First line".
Step 2: Check second command redirection
The second command also uses > which overwrites output.txt, replacing previous content.
Step 3: Correct approach
To keep both lines, second command should use >> to append instead of overwrite.
Final Answer:
Second command overwrites the file instead of appending -> Option B
Quick Check:
Using > twice overwrites file = D [OK]
Hint: Use >> to append, > overwrites file [OK]
Common Mistakes:
Thinking \n missing causes no second line
Confusing > and >> operators
Assuming echo always appends
5. You want to create a file report.txt with exactly two lines: "Summary:" "All tests passed" Which script snippet correctly creates this file with proper newlines and no extra blank lines?
hard
A. printf "Summary:\nAll tests passed\n" > report.txt
B. echo "Summary:" > report.txt && echo "All tests passed" > report.txt
C. echo -n "Summary:\nAll tests passed" > report.txt
D. printf "Summary:" > report.txt && printf "All tests passed" >> report.txt
Solution
Step 1: Check echo commands in echo "Summary:" > report.txt && echo "All tests passed" > report.txt
echo "Summary:" > report.txt && echo "All tests passed" > report.txt uses echo twice with > redirection, so the second command overwrites the first line.
printf "Summary:\nAll tests passed\n" > report.txt uses printf with explicit \n for newlines, creating exactly two lines with no extra blank lines.
Step 3: Analyze Options C and D
echo -n "Summary:\nAll tests passed" > report.txt uses echo -n which suppresses newlines and includes literal \n, so no actual new lines appear. printf "Summary:" > report.txt && printf "All tests passed" >> report.txt uses printf without \n, so lines run together.
Final Answer:
printf "Summary:\nAll tests passed\n" > report.txt -> Option A
Quick Check:
printf with explicit \n creates exact lines = B [OK]
Hint: Use printf with \n for precise multi-line output [OK]
Common Mistakes:
Using echo -n with \n prints literal \n
Forgetting \n in printf causes no new lines
Using > and >> inconsistently
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.