Process control is the way a script manages when and how commands run, including starting, stopping, and waiting for tasks to finish.
Click to reveal answer
beginner
Why do we use process control to manage execution in scripts?
We use process control to make sure commands run in the right order, to handle tasks running at the same time, and to control script flow based on command results.
Click to reveal answer
intermediate
What does the 'wait' command do in bash process control?
The 'wait' command pauses the script until a background task finishes, ensuring the script continues only after that task is done.
Click to reveal answer
beginner
How does running a command with '&' affect process control?
Adding '&' runs the command in the background, letting the script continue without waiting for that command to finish immediately.
Click to reveal answer
intermediate
How does process control help handle errors during script execution?
Process control lets scripts check if commands succeed or fail, and decide what to do next, like stopping or trying another command.
Click to reveal answer
What does the 'wait' command do in bash scripting?
APauses the script until a background process finishes
BStarts a new background process
CStops all running processes immediately
DRestarts the script from the beginning
✗ Incorrect
The 'wait' command pauses the script until the specified background process completes.
What happens when you add '&' at the end of a command in bash?
ARuns the command in debug mode
BRuns the command twice
CRuns the command in the background
DStops the script execution
✗ Incorrect
Adding '&' runs the command in the background, allowing the script to continue without waiting.
Why is process control important in scripting?
ATo change the script's color scheme
BTo manage the order and timing of command execution
CTo increase the script file size
DTo delete all temporary files automatically
✗ Incorrect
Process control manages when and how commands run, ensuring correct script flow.
Which command can you use to check if a previous command succeeded?
Aif [ $? -eq 0 ]
Bwait
Cexit
Dkill
✗ Incorrect
The special variable $? holds the exit status of the last command; 0 means success.
What does process control NOT do?
AControl script execution flow
BManage background and foreground tasks
CWait for tasks to complete
DAutomatically fix syntax errors
✗ Incorrect
Process control manages execution but does not fix syntax errors automatically.
Explain in your own words why process control is important for managing execution in bash scripts.
Think about how scripts run multiple commands and sometimes need to pause or continue.
You got /4 concepts.
Describe how you would use process control to run a command in the background and then wait for it to finish before continuing.
Consider how to let a task run while the script does other things, then pause to check results.
You got /3 concepts.
Practice
(1/5)
1. What does the & symbol do when added at the end of a command in a bash script?
easy
A. Runs the command in the background, allowing the script to continue immediately
B. Stops the script execution until the command finishes
C. Runs the command only if the previous command failed
D. Combines two commands to run one after the other
Solution
Step 1: Understand the role of & in bash
The & symbol placed after 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 running the next commands immediately.
Final Answer:
Runs the command in the background, allowing the script to continue immediately -> Option A
Quick Check:
& runs command in background [OK]
Hint: Background tasks use & to run without waiting [OK]
Common Mistakes:
Thinking & waits for command to finish
Confusing & with && or ||
Assuming & runs command only on failure
2. Which of the following is the correct syntax to run command2 only if command1 succeeds?
echo "Start" runs and succeeds, so next command after && runs.
Step 2: Evaluate the second command
false always fails (exit status non-zero), so the next && echo "Middle" does not run.
Step 3: Evaluate the || echo "End"
Because the chain before || failed, echo "End" runs.
Final Answer:
Start
End -> Option A
Quick Check:
Success && fail skips next && runs || next [OK]
Hint: && stops on failure, || runs if previous failed [OK]
Common Mistakes:
Assuming all commands run sequentially
Ignoring that false always fails
Confusing && and || behavior
4. Identify the error in this script snippet:
sleep 5 &
wait
echo "Done"
medium
A. There is an extra space before echo causing syntax error
B. The ampersand & should be at the end of the wait command
C. The ampersand & is incorrectly placed after sleep 5
D. The wait command is missing a process ID
Solution
Step 1: Examine the wait command
The wait command lacks a process ID argument.
Step 2: Behavior of wait without PID
Without a PID (e.g., $!), wait pauses until all background jobs complete, which may include unintended jobs and cause longer waits.
Step 3: Proper fix
Use sleep 5 & pid=$!; wait $pid or wait $! to target only the sleep job.
Final Answer:
The wait command is missing a process ID -> Option D
Quick Check:
wait w/o PID waits all BG jobs [OK]
Hint: Use wait $! for specific BG job [OK]
Common Mistakes:
Using wait w/o PID when targeting specific job
Misplacing & after wait
Wrongly thinking & after sleep is bad
Believing leading spaces cause syntax errors
5. You want to run three commands: cmd1, cmd2, and cmd3. Run cmd1 and cmd2 in parallel, then run cmd3 only after both finish successfully. Which script snippet achieves this?
hard
A. cmd1 && cmd2 && cmd3 &
B. cmd1 & cmd2 & wait && cmd3
C. cmd1 & cmd2 && wait && cmd3
D. cmd1 & cmd2 & cmd3 && wait
Solution
Step 1: Start cmd1 and cmd2 in background
cmd1 & cmd2 & launches both commands asynchronously in parallel.
Step 2: Wait for both background jobs
wait pauses script execution until both background jobs finish.
Step 3: Run cmd3 after successful completion
wait && cmd3 ensures cmd3 runs only if both background jobs succeed.
Final Answer:
cmd1 & cmd2 & wait && cmd3 -> Option B
Quick Check:
Both BG jobs & wait then conditional cmd3 [OK]
Hint: Run both commands in background, wait, then run next [OK]
Common Mistakes:
Running cmd3 before wait finishes
Not using wait to synchronize background jobs
Placing & incorrectly causing sequential runs
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.