Reading files line by line (while read) in Bash Scripting - Time & Space Complexity
When reading a file line by line in a bash script, it's important to know how the time to finish grows as the file gets bigger.
We want to understand how the script's running time changes when the file has more lines.
Analyze the time complexity of the following code snippet.
while IFS= read -r line; do
echo "$line"
done < input.txt
This script reads each line from a file named input.txt and prints it to the screen.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading and printing each line inside the
while readloop. - How many times: Once for every line in the file.
As the number of lines in the file grows, the script does more reading and printing steps, one per line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 reads and prints |
| 100 | About 100 reads and prints |
| 1000 | About 1000 reads and prints |
Pattern observation: The work grows directly with the number of lines. Double the lines, double the work.
Time Complexity: O(n)
This means the script takes time proportional to the number of lines in the file.
[X] Wrong: "Reading a file line by line is always constant time because it just reads one line at a time."
[OK] Correct: Even though it reads one line at a time, the total time depends on how many lines there are. More lines mean more reads, so time grows with file size.
Understanding how loops over file lines scale helps you explain script efficiency clearly and confidently in real-world tasks and interviews.
"What if we changed the script to read the whole file at once into a variable before processing? How would the time complexity change?"
Practice
while IFS= read -r line; do echo "$line"; done < file.txt do in a bash script?Solution
Step 1: Understand the
The loop reads input line by line from the redirected filewhile readloopfile.txt.Step 2: Check the action inside the loop
Each line read is stored in variablelineand printed withecho.Final Answer:
Reads the filefile.txtline by line and prints each line. -> Option DQuick Check:
Line-by-line reading = A [OK]
while read loops read files line by line [OK]- Thinking it reads the whole file at once
- Confusing input redirection with output
- Ignoring the role of
IFS=and-r
Solution
Step 1: Identify input redirection for reading
The correct way to read from a file is using input redirection:< file.txt.Step 2: Check the loop syntax
while read line; do ... doneis the standard loop to read lines.Final Answer:
while read line; do echo $line; done < file.txt -> Option AQuick Check:
Input redirection with < = C [OK]
< file.txt to feed file lines into the loop [OK]- Using output redirection (>) instead of input (<)
- Piping file name instead of redirecting input
- Missing the
-roption to prevent backslash escapes
file.txt contains:apple banana cherry
Script:
while IFS= read -r line; do echo "Fruit: $line"; done < file.txt
Solution
Step 1: Understand the loop reading each line
The loop reads each line fromfile.txtseparately.Step 2: Check the echo command
Each line is printed prefixed with "Fruit: ".Final Answer:
Fruit: apple Fruit: banana Fruit: cherry -> Option AQuick Check:
Line prefixing with echo = B [OK]
- Expecting all lines printed on one line
- Ignoring the prefix in echo
- Confusing input and output redirection
while read line; do echo $line; done file.txt
Solution
Step 1: Check how the file is provided to the loop
The file must be redirected as input using< file.txt, not as an argument.Step 2: Confirm syntax correctness
The rest of the syntax is correct; variable and echo usage are fine.Final Answer:
Missing input redirection operator < before file.txt -> Option CQuick Check:
Input redirection needed = D [OK]
< file.txt to feed file lines, not argument [OK]- Passing filename as argument instead of redirecting input
- Forgetting semicolon after read (optional here)
- Misusing echo syntax
Solution
Step 1: Understand skipping empty lines
We need to check if the line is not empty using[[ -n $line ]].Step 2: Confirm reading and condition
while IFS= read -r line; do if [[ -n $line ]]; then echo "$line"; fi; done < file.txt reads lines safely and prints only non-empty lines inside the loop.Final Answer:
while IFS= read -r line; do if [[ -n $line ]]; then echo "$line"; fi; done < file.txt -> Option BQuick Check:
Condition inside loop filters empty lines = A [OK]
if [[ -n $line ]] inside loop to skip empty lines [OK]- Using external grep after loop (may lose variable scope)
- Not checking for empty lines inside the loop
- Using unnecessary cat command
Ready to prove it?
Pass the test and this pattern moves unknown
