0
0
Bash Scriptingscripting~5 mins

Process substitution (<() and >()) in Bash Scripting - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Process substitution (<() and >())
O(n log n)
Understanding Time Complexity

We want to understand how the time cost changes when using process substitution in bash scripts.

Specifically, how the script's execution time grows as input size changes when using <() and ()>.

Scenario Under Consideration

Analyze the time complexity of the following bash snippet using process substitution.


    diff <(sort file1.txt) <(sort file2.txt)
    

This code compares two files by sorting each and then running diff on the sorted outputs using process substitution.

Identify Repeating Operations

Look for loops or repeated commands inside the process substitution.

  • Primary operation: Sorting each file with sort, which reads and processes all lines.
  • How many times: Each file is processed once by sort, then diff compares the sorted outputs line by line.
How Execution Grows With Input

As the size of each file grows, the sorting and diffing take more time.

Input Size (n lines per file)Approx. Operations
10Sort ~10 lines + diff ~10 lines
100Sort ~100 lines + diff ~100 lines
1000Sort ~1000 lines + diff ~1000 lines

Pattern observation: Sorting cost grows faster than line count, diff grows roughly linearly.

Final Time Complexity

Time Complexity: O(n log n)

This means the script's time mainly depends on sorting each file, which grows a bit faster than just reading lines.

Common Mistake

[X] Wrong: "Process substitution runs instantly and does not add to time complexity."

[OK] Correct: Process substitution runs the commands inside it fully, so their time cost counts just like normal commands.

Interview Connect

Understanding how process substitution affects script time helps you write efficient bash scripts and explain your choices clearly in interviews.

Self-Check

What if we replaced sort with a command that reads the file once without sorting? How would the time complexity change?