Recall & Review
beginner
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?
✗ 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?✗ Incorrect
The > operator overwrites the file content with the new text.
Which command gives more control over formatting when writing to files?
✗ Incorrect
printf allows precise formatting like newlines and tabs.How do you write multiple lines to a file using
echo?✗ Incorrect
All these methods can write multiple lines, but some require multiple commands.
What does the command
printf "Hello\nWorld" > file.txt do?✗ Incorrect
The \n creates a new line, so 'Hello' and 'World' appear on separate lines.
Explain how to write and append text to a file using bash commands.
Think about how you save notes in a notebook: writing fresh or adding more.
You got /3 concepts.
Describe when and why you would use printf instead of echo for writing to files.
Consider needing exact layout or special characters in your text.
You got /3 concepts.