Appending to files (>>) in Bash Scripting - Time & Space Complexity
When we append text to a file using bash, we want to know how the time it takes changes as the file grows.
We ask: Does adding more lines make the append slower, and by how much?
Analyze the time complexity of the following code snippet.
for ((i=1; i<=n; i++)); do
echo "Line $i" >> output.txt
done
This script appends n lines to a file named output.txt, one line at a time.
- Primary operation: Appending one line to the file using
>>inside a loop. - How many times: Exactly n times, once per loop iteration.
Each append adds one line, and the loop runs n times, so the total work grows as we add more lines.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 appends |
| 100 | 100 appends |
| 1000 | 1000 appends |
Pattern observation: The time grows directly with the number of lines added.
Time Complexity: O(n)
This means the time to append grows in a straight line as you add more lines.
[X] Wrong: "Appending to a file is instant no matter how big the file is."
[OK] Correct: Each append operation takes some time, so doing many appends adds up and takes longer as n grows.
Understanding how loops and file operations scale helps you explain script efficiency clearly and confidently.
What if we collected all lines in a variable and wrote them to the file once? How would the time complexity change?
Practice
>> operator do in bash scripting?Solution
Step 1: Understand the operator function
The>>operator appends data to a file, meaning it adds new content at the end without removing what is already there.Step 2: Compare with other operators
Unlike>which overwrites,>>keeps existing content and adds more.Final Answer:
It adds new content to the end of a file without deleting existing data. -> Option AQuick Check:
>>appends content [OK]
- Confusing >> with > which overwrites files
- Thinking >> deletes the file first
- Assuming >> reads file content
greetings.txt?Solution
Step 1: Identify the append operator
To add text to the end of a file, use>>after the command.Step 2: Match syntax with the command
echo Hello >> greetings.txtappends "Hello" to the file without deleting existing content.Final Answer:
echo Hello >> greetings.txt -> Option CQuick Check:
Use>>to append text [OK]
- Using single > which overwrites the file
- Using < which is for input redirection
- Using pipe | incorrectly to write to file
log.txt after running these commands?echo "Start" > log.txt echo "Step 1" >> log.txt echo "Step 2" >> log.txt
Solution
Step 1: Analyze the first command
echo "Start" > log.txtcreates or overwriteslog.txtwith "Start".Step 2: Analyze the next two append commands
Bothecho "Step 1" >> log.txtandecho "Step 2" >> log.txtadd lines at the end without deleting existing content.Final Answer:
Start Step 1 Step 2 -> Option DQuick Check:
First > overwrites, then >> appends [OK]
- Thinking >> overwrites file
- Ignoring newline added by echo
- Assuming only last echo remains
backup.log, but your script uses echo "Backup complete" > backup.log. What is the problem?Solution
Step 1: Understand the operator used
The single>operator overwrites the file content, replacing it with new text.Step 2: Identify correct operator for appending
To add text without deleting existing content,>>should be used instead.Final Answer:
The command overwrites the file instead of appending. -> Option BQuick Check:
Use>>to append, not>[OK]
- Using > when >> is needed
- Expecting append but file is replaced
- Thinking > causes syntax error
actions.log. You want to ensure the file is created if missing and new actions are added without deleting old logs. Which command correctly appends the text "User logged in" to the file?Solution
Step 1: Confirm file creation and appending
The>>operator appends text and creates the file if it does not exist.Step 2: Validate command correctness
echo "User logged in" >> actions.logappends the message correctly without deleting old logs.Final Answer:
echo "User logged in" >> actions.log -> Option AQuick Check:
>>appends and creates file if missing [OK]
- Using > which overwrites logs
- Using < which is input redirection
- Using cat incorrectly to append text
Ready to prove it?
Pass the test and this pattern moves unknown
