Parallel execution patterns in Bash Scripting - Time & Space Complexity
When running tasks in parallel in bash scripts, it is important to understand how the total time changes as we add more tasks.
We want to know how the execution time grows when multiple commands run at the same time.
Analyze the time complexity of the following bash script using parallel execution.
for file in *.txt; do
cat "$file" &
done
wait
This script runs the cat command on each text file in the folder at the same time, then waits for all to finish.
Look for repeated actions that take time.
- Primary operation: Running
caton each file. - How many times: Once per file, all started in parallel.
Since all commands run at the same time, total time depends mostly on the longest single command.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Time of longest cat among 10 files |
| 100 | Time of longest cat among 100 files |
| 1000 | Time of longest cat among 1000 files |
Pattern observation: Total time grows with the longest single task, not the number of tasks.
Time Complexity: O(1)
This means the total time stays about the same no matter how many tasks run, assuming enough resources to run all in parallel.
[X] Wrong: "Running more tasks in parallel always takes more time overall."
[OK] Correct: When tasks run truly in parallel, total time depends on the longest task, not the count of tasks.
Understanding how parallel execution affects time helps you write faster scripts and shows you can think about efficiency in real situations.
What if we limit the number of parallel tasks to 5 at a time? How would the time complexity change?
Practice
& at the end of a command in a bash script do?Solution
Step 1: Understand the role of
The&in bash&symbol placed at the end of a command tells bash to run that command in the background.Step 2: Effect on script execution
Running a command in the background means the script does not wait for it to finish and continues to the next command immediately.Final Answer:
Runs the command in the background, allowing the script to continue immediately -> Option AQuick Check:
Background execution = & at end [OK]
- Thinking & pauses the script until command finishes
- Confusing & with output redirection
- Assuming & changes command priority
Solution
Step 1: Identify how to run commands in parallel
Appending&to a command runs it in the background. To run two commands in parallel, both must have&.Step 2: Analyze options
command1 & command2 &runs both commands with&, so both run in background simultaneously.command1 && command2 &uses&&which means run second only if first succeeds, then runs second in background (but first runs in foreground).command1 && command2uses&&without background.command1 & command2runs command1 in background but command2 in foreground, so the script blocks until command2 finishes.Final Answer:
command1 & command2 &-> Option AQuick Check:
Both commands need & to run parallel [OK]
- Using && which runs commands sequentially
- Missing & on second command
- Confusing & with &&
echo "Start" sleep 2 & echo "Middle" sleep 1 & echo "End" wait
Solution
Step 1: Understand background jobs and output
Thesleepcommands run in background due to&, so they do not block the script. Theechocommands run immediately in order.Step 2: Analyze output order
The script prints "Start", then starts sleep 2 in background, then prints "Middle", starts sleep 1 in background, then prints "End". Thewaitpauses script exit until sleeps finish but does not affect output order.Final Answer:
Start\nMiddle\nEnd (then waits for sleeps to finish) -> Option CQuick Check:
Background jobs don't block echo output [OK]
- Assuming wait delays echo output
- Thinking sleep blocks echo
- Confusing order of background jobs
command1 & command2 wait
Solution
Step 1: Check background execution of commands
Onlycommand1has&to run in background.command2runs in foreground, blocking until it finishes.Step 2: Effect on wait
Thewaitcommand waits for all background jobs to finish. Since onlycommand1is background,waitwaits only for it.command2runs before wait.Final Answer:
command2 is not run in background, so wait only waits for command1 -> Option DQuick Check:
All parallel commands need & before wait [OK]
- Forgetting & on second command
- Thinking wait needs arguments
- Assuming wait waits for foreground commands
script1.sh, script2.sh, and script3.sh in parallel and then print "All done" only after all finish. Which is the best way to do this?Solution
Step 1: Run all scripts in background
Adding&after each script runs them in parallel without waiting.Step 2: Use wait to pause until all finish
Thewaitcommand pauses the script until all background jobs complete, ensuring synchronization.Step 3: Print message after all finish
Afterwait,echo "All done"runs, confirming all scripts finished.Final Answer:
./script1.sh & ./script2.sh & ./script3.sh & wait echo "All done"
-> Option BQuick Check:
Background + wait + echo after = correct pattern [OK]
- Using && runs scripts sequentially
- Missing wait causes echo to run too early
- Putting echo on same line with & causes immediate print
Ready to prove it?
Pass the test and this pattern moves unknown
