wait
wait is like standing at the finish line until your friends who ran off in the background come back. The script pauses until they have all finished.
In this page:
Syntax
command &
wait [process_id]
wait
wait pauses a script until a background job finishes, or until all background jobs finish if called with no arguments. wait $pid waits for one specific job and captures its exit status.
This is essential for coordinating multiple background tasks before moving on.
Note:
Call
wait with no arguments to pause until *every* background job the script started has completed.Example: wait
#!/bin/bash
sleep 1 &
sleep 1 &
echo "Waiting for all background jobs..."
wait
echo "All background jobs finished"
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Calling
waitwith no PID and expecting the exit status of a specific job. - Forgetting
&on the command, so there is nothing to wait for. - Waiting for a PID that is not a child of the current shell.
🔒
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: