📖 Scenario: You are managing a server where you need to run multiple tasks at the same time to save time. Running tasks one after another takes too long. You want to learn how to run commands in parallel using Bash scripting.
🎯 Goal: Build a Bash script that runs three simple commands in parallel and waits for all of them to finish before showing a message.
📋 What You'll Learn
Create three simple commands that each sleep for a few seconds
Run these commands in parallel using background execution
Use a variable to count how many commands are running
Wait for all commands to finish before printing a completion message
💡 Why This Matters
🌍 Real World
Running multiple tasks at the same time saves time on servers and automation scripts. For example, downloading files or processing data in parallel.
💼 Career
System administrators and DevOps engineers often use parallel execution in Bash scripts to improve efficiency and manage multiple processes.
Progress0 / 4 steps
1
Create three simple commands
Create three variables called cmd1, cmd2, and cmd3. Set cmd1 to sleep 3, cmd2 to sleep 4, and cmd3 to sleep 2.
Bash Scripting
Hint
Use simple assignment like cmd1="sleep 3" to store commands as strings.
2
Add a counter for running commands
Create a variable called running and set it to 0. This will count how many commands are running in parallel.
Bash Scripting
Hint
Initialize the counter with running=0.
3
Run commands in parallel and update counter
Use $cmd1 &, $cmd2 &, and $cmd3 & to run the commands in the background. After each command, increase running by 1 using running=$((running + 1)).
Bash Scripting
Hint
Run each command with & at the end to run in background. Increase running after each.
4
Wait for all commands and print completion message
Use the wait command to wait for all background commands to finish. Then print echo "All $running commands finished." to show the message.
Bash Scripting
Hint
Use wait to pause until all background jobs finish, then print the message.
Practice
(1/5)
1. What does adding & at the end of a command in a bash script do?
easy
A. Runs the command in the background, allowing the script to continue immediately
B. Stops the script until the command finishes
C. Redirects the command output to a file
D. Runs the command with higher priority
Solution
Step 1: Understand the role of & in bash
The & 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 A
Quick Check:
Background execution = & at end [OK]
Hint: Remember & means run in background, no waiting [OK]
Common Mistakes:
Thinking & pauses the script until command finishes
Confusing & with output redirection
Assuming & changes command priority
2. Which of the following is the correct syntax to run two commands in parallel in bash?
easy
A. command1 & command2 &
B. command1 && command2 &
C. command1 && command2
D. command1 & command2
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 && command2 uses && without background. command1 & command2 runs command1 in background but command2 in foreground, so the script blocks until command2 finishes.
Final Answer:
command1 & command2 & -> Option A
Quick Check:
Both commands need & to run parallel [OK]
Hint: Use & after each command to run both in parallel [OK]
Common Mistakes:
Using && which runs commands sequentially
Missing & on second command
Confusing & with &&
3. What will be the output order of the following script?
A. Start\nMiddle\nEnd\n(wait blocks output until sleeps finish)
B. Start\n(wait)\nMiddle\nEnd
C. Start\nMiddle\nEnd (then waits for sleeps to finish)
D. Start\nEnd\nMiddle
Solution
Step 1: Understand background jobs and output
The sleep commands run in background due to &, so they do not block the script. The echo commands 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". The wait pauses script exit until sleeps finish but does not affect output order.
Final Answer:
Start\nMiddle\nEnd (then waits for sleeps to finish) -> Option C
Quick Check:
Background jobs don't block echo output [OK]
Hint: Background jobs run silently; echo prints immediately [OK]
Common Mistakes:
Assuming wait delays echo output
Thinking sleep blocks echo
Confusing order of background jobs
4. Identify the error in this script snippet that tries to run two commands in parallel and wait for both:
command1 &
command2
wait
medium
A. command1 & syntax is incorrect
B. wait command is missing arguments
C. Both commands run sequentially, no parallelism
D. command2 is not run in background, so wait only waits for command1
Solution
Step 1: Check background execution of commands
Only command1 has & to run in background. command2 runs in foreground, blocking until it finishes.
Step 2: Effect on wait
The wait command waits for all background jobs to finish. Since only command1 is background, wait waits only for it. command2 runs before wait.
Final Answer:
command2 is not run in background, so wait only waits for command1 -> Option D
Quick Check:
All parallel commands need & before wait [OK]
Hint: Add & to all parallel commands before wait [OK]
Common Mistakes:
Forgetting & on second command
Thinking wait needs arguments
Assuming wait waits for foreground commands
5. You want to run 3 independent scripts 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?
hard
A.
./script1.sh && ./script2.sh && ./script3.sh && echo "All done"
B.
./script1.sh &
./script2.sh &
./script3.sh &
wait
echo "All done"
C.
./script1.sh & ./script2.sh & ./script3.sh & echo "All done"
D.
./script1.sh &
./script2.sh &
./script3.sh
echo "All done"
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
The wait command pauses the script until all background jobs complete, ensuring synchronization.
Step 3: Print message after all finish
After wait, echo "All done" runs, confirming all scripts finished.
Final Answer:
./script1.sh &
./script2.sh &
./script3.sh &
wait
echo "All done"
-> Option B
Quick Check:
Background + wait + echo after = correct pattern [OK]
Hint: Use & for parallel, then wait before next step [OK]
Common Mistakes:
Using && runs scripts sequentially
Missing wait causes echo to run too early
Putting echo on same line with & causes immediate print
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.