0
0
Bash Scriptingscripting~10 mins

Parallel execution patterns in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Parallel execution patterns
Start script
Launch commands in background (&)
Commands run in parallel
Wait for all to finish (wait)
Continue with next steps
Script ends
The script starts, launches multiple commands in the background to run at the same time, waits for all to finish, then continues.
Execution Sample
Bash Scripting
echo "Start"
sleep 2 &
sleep 3 &
wait
echo "Done"
Runs two sleep commands in parallel, waits for both to finish, then prints 'Done'.
Execution Table
StepActionCommandBackground?OutputNotes
1Print start messageecho "Start"NoStartPrints immediately
2Launch first sleepsleep 2YesRuns in background
3Launch second sleepsleep 3YesRuns in background
4Wait for all background jobswaitNoPauses until both sleeps finish
5Print done messageecho "Done"NoDoneRuns after both sleeps finish
💡 All background jobs complete, wait ends, script continues
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
Background jobs count01200
Key Moments - 2 Insights
Why does the script print 'Done' only after both sleep commands finish?
Because the 'wait' command pauses the script until all background jobs complete, as shown in step 4 of the execution_table.
What happens if we omit the '&' after the sleep commands?
Without '&', the commands run one after another, not in parallel, so the script waits for each sleep to finish before starting the next, unlike steps 2 and 3 where '&' runs them in background.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the script wait for background jobs to finish?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Check the 'Action' and 'Command' columns in execution_table row 4.
According to variable_tracker, how many background jobs are running after step 3?
A0
B1
C2
D3
💡 Hint
Look at 'Background jobs count' after Step 3 in variable_tracker.
If we remove the 'wait' command, what will happen to the output order?
A'Done' prints before sleeps finish
B'Done' prints after sleeps finish
CScript will hang forever
DScript will print nothing
💡 Hint
Refer to the role of 'wait' in execution_table step 4.
Concept Snapshot
Parallel execution in bash uses '&' to run commands in background.
Use 'wait' to pause script until all background jobs finish.
Without '&', commands run one by one.
This pattern speeds up scripts by running tasks simultaneously.
Full Transcript
This example shows how to run commands in parallel in bash. First, the script prints 'Start'. Then it launches two sleep commands with '&' so they run at the same time in the background. The script uses 'wait' to pause until both sleep commands finish. After that, it prints 'Done'. The variable tracker shows how background jobs increase when commands start and reset after wait. Key points are that '&' runs commands in parallel and 'wait' pauses until all finish. Without '&', commands run sequentially. Without 'wait', the script prints 'Done' immediately without waiting. This pattern helps run tasks faster by doing them at the same time.