← Back to Bash Course | Chapter 10: Text Processing Tools | Lesson 4 of 12

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:

  1. wait
Syntax
bash
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

bash
#!/bin/bash
sleep 1 &
sleep 1 &

echo "Waiting for all background jobs..."
wait
echo "All background jobs finished"
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
  1. Calling wait with no PID and expecting the exit status of a specific job.
  2. Forgetting & on the command, so there is nothing to wait for.
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.