Writing to files (echo, printf) in Bash Scripting - Time & Space Complexity
When writing data to files using bash commands like echo or printf, it is important to understand how the time taken grows as the amount of data increases.
We want to know how the execution time changes when writing more lines or bigger content to a file.
Analyze the time complexity of the following code snippet.
for i in $(seq 1 $n); do
echo "Line $i" >> output.txt
printf "Number: %d\n" "$i" >> output.txt
done
This script writes two lines to a file for each number from 1 to n.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
forloop runsntimes. - How many times: Each iteration writes two lines to the file using
echoandprintf.
As n increases, the number of write operations grows directly with it.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 20 writes |
| 100 | 200 writes |
| 1000 | 2000 writes |
Pattern observation: The total writes increase linearly as n grows.
Time Complexity: O(n)
This means the time to write grows in direct proportion to the number of lines you want to write.
[X] Wrong: "Writing multiple lines inside a loop is constant time because each write is fast."
[OK] Correct: Each write takes time, so more lines mean more total time. The time adds up linearly, not stays the same.
Understanding how writing to files scales helps you reason about script performance and resource use in real tasks.
"What if we buffered all lines in a variable and wrote once at the end? How would the time complexity change?"
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
