Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Parallel execution patterns in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Parallel Execution Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of background jobs with wait
What will be the output of this script when run in a bash shell?
Bash Scripting
echo "Start"
(sleep 1; echo "Job 1 done") &
(sleep 2; echo "Job 2 done") &
wait
echo "All jobs finished"
AStart\nAll jobs finished\nJob 1 done\nJob 2 done
BStart\nJob 1 done\nJob 2 done\nAll jobs finished
CJob 1 done\nJob 2 done\nStart\nAll jobs finished
DStart\nJob 2 done\nJob 1 done\nAll jobs finished
Attempts:
2 left
💡 Hint
Remember that 'wait' pauses the script until all background jobs finish.
📝 Syntax
intermediate
1:30remaining
Correct syntax for running commands in parallel
Which option correctly runs three commands in parallel and waits for all to finish?
Acmd1 & cmd2 & cmd3 & wait &
Bcmd1 & cmd2 & cmd3 wait
Ccmd1 & cmd2 & cmd3 & wait
Dcmd1 cmd2 cmd3 & wait
Attempts:
2 left
💡 Hint
Use '&' to run commands in background and 'wait' to pause until all finish.
🔧 Debug
advanced
2:00remaining
Fix the issue with parallel job output mixing
This script runs two background jobs but their outputs sometimes mix on the terminal. What is the main cause?
Bash Scripting
echo "Start"
(sleep 1; echo "Job 1 done") &
(sleep 1; echo "Job 2 done") &
wait
echo "End"
AThe sleep times are different causing output order issues.
BThe 'wait' command is missing causing jobs to overlap.
CThe echo commands are missing quotes causing syntax errors.
DBoth jobs write to stdout simultaneously causing output mixing.
Attempts:
2 left
💡 Hint
Think about how multiple background jobs write to the terminal at the same time.
🚀 Application
advanced
2:30remaining
Limit number of parallel jobs in a loop
You want to run 10 commands in parallel but only allow 3 to run at the same time. Which approach achieves this?
Bash Scripting
for i in {1..10}; do
  some_command $i &
  # limit to 3 parallel jobs
  # ???
done
wait
AUse a counter and 'wait' every 3 jobs: if (( i % 3 == 0 )); then wait; fi
BRun all commands in background without wait to limit jobs
CUse 'sleep 3' inside the loop to delay jobs
DUse 'wait' only after the loop to limit jobs
Attempts:
2 left
💡 Hint
You can pause the loop every few jobs to limit concurrency.
🧠 Conceptual
expert
3:00remaining
Understanding job control and process IDs in parallel scripts
In a bash script, you run multiple background jobs and want to track their process IDs (PIDs) to wait for specific ones. Which method correctly captures and waits for each job's PID?
A
cmd1 &
pid1=$!
cmd2 &
pid2=$!
wait $pid1
wait $pid2
B
cmd1 & pid1=$!
cmd2 & pid2=$!
wait $pid1
wait $pid2
C
pid1=$!; cmd1
pid2=$!; cmd2
wait $pid1
wait $pid2
D
pid1=$!; cmd1 &
pid2=$!; cmd2 &
wait $pid1
wait $pid2
Attempts:
2 left
💡 Hint
The special variable $! holds the PID of the last background job started.

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

  1. 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.
  2. 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.
  3. Final Answer:

    Runs the command in the background, allowing the script to continue immediately -> Option A
  4. 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

  1. 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 &.
  2. 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.
  3. Final Answer:

    command1 & command2 & -> Option A
  4. 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?
echo "Start"
sleep 2 &
echo "Middle"
sleep 1 &
echo "End"
wait
medium
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

  1. 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.
  2. 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.
  3. Final Answer:

    Start\nMiddle\nEnd (then waits for sleeps to finish) -> Option C
  4. 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

  1. Step 1: Check background execution of commands

    Only command1 has & to run in background. command2 runs in foreground, blocking until it finishes.
  2. 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.
  3. Final Answer:

    command2 is not run in background, so wait only waits for command1 -> Option D
  4. 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

  1. Step 1: Run all scripts in background

    Adding & after each script runs them in parallel without waiting.
  2. Step 2: Use wait to pause until all finish

    The wait command pauses the script until all background jobs complete, ensuring synchronization.
  3. Step 3: Print message after all finish

    After wait, echo "All done" runs, confirming all scripts finished.
  4. Final Answer:

    ./script1.sh &
    ./script2.sh &
    ./script3.sh &
    wait
    echo "All done"
    -> Option B
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard