0
0
Bash Scriptingscripting~5 mins

Writing to files (echo, printf) in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
A>
B<
C|
D>>
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
Which command gives more control over formatting when writing to files?
Aecho
Bcat
Cprintf
Dls
How do you write multiple lines to a file using echo?
Aecho "Line1" > file.txt; echo "Line2" >> file.txt
BAll of the above
Cecho "Line1" >> file.txt; echo "Line2" >> file.txt
Decho -e "Line1\nLine2" > file.txt
What does the command printf "Hello\nWorld" > file.txt do?
AWrites 'Hello' and 'World' on two separate lines
BWrites 'Hello World' on one line
CAppends 'Hello' and 'World' to the file
DShows an error
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.