Bird
0
0

You want to create a file report.txt with exactly two lines:

hard🚀 Application Q15 of 15
Bash Scripting - File Operations in Scripts
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?
Aprintf "Summary:\nAll tests passed\n" > report.txt
Becho "Summary:" > report.txt && echo "All tests passed" > report.txt
Cecho -n "Summary:\nAll tests passed" > report.txt
Dprintf "Summary:" > report.txt && printf "All tests passed" >> report.txt
Step-by-Step Solution
Solution:
  1. 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.
  2. Step 2: Check printf commands in printf "Summary:\nAll tests passed\n" > report.txt

    printf "Summary:\nAll tests passed\n" > report.txt uses printf with explicit \n for newlines, creating exactly two lines with no extra blank lines.
  3. 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.
  4. Final Answer:

    printf "Summary:\nAll tests passed\n" > report.txt -> Option A
  5. Quick Check:

    printf with explicit \n creates exact lines = B [OK]
Quick Trick: Use printf with \n for precise multi-line output [OK]
Common Mistakes:
MISTAKES
  • Using echo -n with \n prints literal \n
  • Forgetting \n in printf causes no new lines
  • Using > and >> inconsistently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes