0
0
Bash Scriptingscripting~5 mins

Reading files line by line (while read) in Bash Scripting - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Reading files line by line (while read)
O(n)
Understanding Time 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.

Scenario Under Consideration

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 Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Reading and printing each line inside the while read loop.
  • How many times: Once for every line in the file.
How Execution Grows With Input

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
10About 10 reads and prints
100About 100 reads and prints
1000About 1000 reads and prints

Pattern observation: The work grows directly with the number of lines. Double the lines, double the work.

Final Time Complexity

Time Complexity: O(n)

This means the script takes time proportional to the number of lines in the file.

Common Mistake

[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.

Interview Connect

Understanding how loops over file lines scale helps you explain script efficiency clearly and confidently in real-world tasks and interviews.

Self-Check

"What if we changed the script to read the whole file at once into a variable before processing? How would the time complexity change?"