The wait command is a shell builtin that pauses script execution until a specific background process, or all running child processes, have finished.
Its primary purpose is to synchronize tasks, ensuring that a script doesn't continue to the next step until prerequisite background jobs are complete. A background process is a command that is run with an ampersand (&) at the end, which tells the shell to run it without waiting for it to finish.
$ wait [PID][PID] - An optional Process ID to wait for. If no PID is given, wait will wait for all active child processes to complete.
This example shows how to launch a single background process and wait for it specifically.
#!/bin/bash
echo "This process will run in the background..." &
process_id=$!
echo "Script is now waiting for process ID: $process_id"
wait $process_id
echo "Process $process_id has finished."
echo "The script exited with status: $?"-
&: The ampersand runs the echo command in the background, allowing the script to immediately continue to the next line.
-
$!: This is a special shell variable that holds the Process ID (PID) of the most recently executed background command. We save it to the process_id variable.
-
wait $process_id: This is the key command. The script pauses here until the process with that specific ID is complete.
-
$?: This variable holds the exit status of the last command that finished. An exit status of 0 means success.
$ bash wait_example.sh
Script is now waiting for process ID: 12345
This process will run in the background...
Process 12345 has finished.
The script exited with status: 0This is the most common use case. Here, we launch several background tasks and then use a single wait command to pause until all of them are done.
#!/bin/bash
echo "Starting multiple background jobs..."
sleep 3 &
sleep 1 &
sleep 2 &
echo "Waiting for all sleep commands to finish."
wait
echo "All jobs are done. Continuing with the rest of the script."$ bash wait_all_example.sh
Starting multiple background jobs...
Waiting for all sleep commands to finish.
(after about 3 seconds)
All jobs are done. Continuing with the rest of the script.