Writing to files lets you save information from your script so you can use it later or share it.
Writing to files (echo, printf) in Bash Scripting
echo "text" > filename printf "format" > filename
echo adds a newline automatically at the end.
printf gives more control over formatting but needs explicit newlines.
file.txt, replacing any existing content.echo "Hello World" > file.txtfile.txt without removing existing content.echo "Line 1" >> file.txtinfo.txt.printf "Name: %s\nAge: %d\n" "Alice" 30 > info.txt
This script writes three lines to output.txt. It first writes a start message, then adds user info with printf, and finally adds a finish message. The cat command shows the file content.
#!/bin/bash echo "Starting the script..." > output.txt printf "User: %s\nScore: %d\n" "Bob" 85 >> output.txt echo "Script finished." >> output.txt cat output.txt
Use >> to add to a file without erasing it.
Remember to give your script permission to write to the file location.
Use quotes around text to avoid problems with spaces or special characters.
Use echo or printf to write text to files in bash.
echo is simple and adds a newline automatically.
printf is more flexible for formatting but needs explicit newlines.
Practice
Hello World to a file named greeting.txt and adds a newline automatically?Solution
Step 1: Understand echo behavior
Theechocommand prints text and adds a newline by default.Step 2: Compare with printf
printfrequires explicit newline characters to add a newline.Final Answer:
echo "Hello World" > greeting.txt -> Option AQuick Check:
echo adds newline automatically = A [OK]
- Using printf without \n misses newline
- Using echo -n suppresses newline
- Confusing > and >> operators
Log entry to a file log.txt using printf?Solution
Step 1: Understand printf newline requirement
printfdoes not add newline automatically; you must include \n explicitly.Step 2: Check append operator
>>appends to the file without overwriting.Final Answer:
printf "Log entry\n" >> log.txt -> Option CQuick Check:
printf needs \n for newline + append with >> = C [OK]
- Forgetting \n causes no newline
- Using > instead of >> overwrites file
- Using invalid printf flags like -n or -e
file.txt after running this script?echo "Line1" > file.txt printf "Line2\n" >> file.txt echo -n "Line3" >> file.txt
Solution
Step 1: Analyze echo "Line1" > file.txt
This writes "Line1" plus newline to file.txt, overwriting existing content.Step 2: Analyze printf "Line2\n" >> file.txt
This appends "Line2" plus newline to file.txt.Step 3: Analyze echo -n "Line3" >> file.txt
This appends "Line3" without newline to file.txt.Final Answer:
Line1\nLine2\nLine3 -> Option DQuick Check:
echo adds newline, printf adds newline, echo -n no newline = D [OK]
- Assuming echo -n adds newline
- Forgetting printf needs \n
- Confusing overwrite (>) and append (>>)
output.txt but only writes one line. What is the error?echo "First line" > output.txt printf "Second line" > output.txt
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 BQuick Check:
Using > twice overwrites file = D [OK]
- Thinking \n missing causes no second line
- Confusing > and >> operators
- Assuming echo always appends
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?
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.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.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 AQuick Check:
printf with explicit \n creates exact lines = B [OK]
- Using echo -n with \n prints literal \n
- Forgetting \n in printf causes no new lines
- Using > and >> inconsistently
Ready to prove it?
Pass the test and this pattern moves unknown
