What does the command echo "Hello" > file.txt do in bash?
It writes the text "Hello" into the file named file.txt. If the file exists, it replaces its content. If not, it creates the file.
Click to reveal answer
beginner
How do you append text to a file using echo?
Use the append operator >> like echo "More text" >> file.txt. This adds the text at the end without deleting existing content.
Click to reveal answer
intermediate
Why might you use printf instead of echo for writing to files?
printf gives more control over formatting, like adding new lines, tabs, or controlling number formats, which echo may handle inconsistently across systems.
Click to reveal answer
beginner
What is the difference between echo "text" > file.txt and echo "text" >> file.txt?
The first command overwrites the file with "text". The second command adds "text" to the end of the file without removing existing content.
Click to reveal answer
intermediate
How can you write multiple lines to a file using printf?
Use printf with newline characters, for example: printf "Line1\nLine2\n" > file.txt writes two lines into the file.
Click to reveal answer
Which operator is used to append text to a file in bash?
A>
B<
C|
D>>
✗ Incorrect
The operator >> appends text to the end of a file without overwriting existing content.
What will echo "Hello" > file.txt do if file.txt already contains text?
AOverwrite the file with "Hello"
BShow an error
CDelete the file
DAdd "Hello" at the end
✗ Incorrect
The > operator overwrites the file content with the new text.
Which command gives more control over formatting when writing to files?
Aecho
Bcat
Cprintf
Dls
✗ Incorrect
printf allows precise formatting like newlines and tabs.
How do you write multiple lines to a file using echo?
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.