Process substitution (<() and >()) in Bash Scripting - Time & Space 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 ()>.
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.
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, thendiffcompares the sorted outputs line by line.
As the size of each file grows, the sorting and diffing take more time.
| Input Size (n lines per file) | Approx. Operations |
|---|---|
| 10 | Sort ~10 lines + diff ~10 lines |
| 100 | Sort ~100 lines + diff ~100 lines |
| 1000 | Sort ~1000 lines + diff ~1000 lines |
Pattern observation: Sorting cost grows faster than line count, diff grows roughly linearly.
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.
[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.
Understanding how process substitution affects script time helps you write efficient bash scripts and explain your choices clearly in interviews.
What if we replaced sort with a command that reads the file once without sorting? How would the time complexity change?
Practice
<() do in bash scripting?Solution
Step 1: Understand process substitution
This syntax runs a command and makes its output appear as a file that another command can read.<()Step 2: Compare with other options
Unlike redirecting output to a disk file,<()creates a temporary file descriptor for input.Final Answer:
It runs a command and provides its output as a file input to another command. -> Option DQuick Check:
Process substitution<()= command output as input file [OK]
- Confusing <() with output redirection >
- Thinking it creates a real disk file
- Assuming it waits for user input
file1 and file2 using diff?Solution
Step 1: Identify correct process substitution usage
Using<()runs commands and provides their output as files todiff.Step 2: Check each option
diff <(cat file1) <(cat file2) correctly usescat file1andcat file2inside<(). Options B, C, and D misuse syntax or operators.Final Answer:
diff <(cat file1) <(cat file2) -> Option AQuick Check:
Correct syntax for process substitution in diff = diff <(cat file1) <(cat file2) [OK]
- Using >() instead of <() for input
- Trying to put filenames directly inside <()
- Mixing redirection symbols incorrectly
comm -12 <(sort fileA.txt) <(sort fileB.txt)
Assuming
fileA.txt contains:apple
banana
carrotand
fileB.txt contains:banana
carrot
dateSolution
Step 1: Understand
The command compares sorted files and outputs lines common to both (options 1 and 2).comm -12with process substitutionStep 2: Sort and compare file contents
SortedfileA.txt: apple, banana, carrot
SortedfileB.txt: banana, carrot, date
Common lines: banana, carrotFinal Answer:
banana carrot -> Option BQuick Check:
Common sorted lines = banana and carrot [OK]
- Not sorting files before comm
- Expecting all lines combined
- Confusing comm options
grep 'error' >(sort > errors_sorted.txt)
Solution
Step 1: Analyze the use of >() in the command
The syntax>()creates a writable file descriptor for output redirection, but heregrepexpects a file input, not output.Step 2: Determine correct substitution type
To provide input from a command,<()should be used. Using>()here is incorrect.Final Answer:
Using >() where <() is needed for input -> Option AQuick Check:
Input needs <() not >() [OK]
- Confusing input and output process substitution
- Assuming >() can be nested inside commands
- Ignoring command expectations for input/output
command1 <(producer) >(consumer)
What does this setup do?
Solution
Step 1: Understand
<()and>()roles<()provides command output as input file,>()provides a writable file for output redirection.Step 2: Analyze the command flow
produceroutput feedscommand1input via<(), andcommand1output is sent toconsumervia>().Final Answer:
Runs producer and feeds its output as input to command1, then sends command1 output to consumer. -> Option CQuick Check:
<()input,>()output redirection [OK]
- Mixing input and output substitutions
- Assuming temporary disk files are created
- Confusing command execution order
Ready to prove it?
Pass the test and this pattern moves unknown
