In Bash scripting, the wait command is commonly used when running commands in the background using the &
symbol, allowing multiple tasks to execute concurrently. However, when the completion of one or more of those tasks is crucial for the next steps, the script must pause and wait.
This is where the ‘wait’ command comes in. It halts the script’s execution until a specific background process, or all background processes, have completed. This ensures smooth automation by preventing race conditions and execution conflicts.
Whether you’re downloading files, starting services, or managing timed operations, the ‘wait’ command helps keep reliable control over the flow of your script.
Prerequisites
- A Linux environment or terminal with Bash installed
- Basic knowledge of shell scripting
- Access to create and execute ‘.sh‘ script files
Bash ‘wait’ Command Syntax and Common Options
You can use the ‘wait’ command in Bash with this basic syntax:
wait [pid ...]
Option | Description |
---|---|
wait | Waits for all background jobs to complete. |
wait PID | Waits only for the job with the specified PID. |
$! | Holds the PID of the last background command. |
$? | Stores the exit status of the last completed command. |
Note: If no PID or job ID is specified, ‘wait’ pauses until all background processes are finished.
Single Process ‘wait’ Command
Description
When you use ‘&’, a command runs in the background. By default, the script continues executing. To pause the script until that background task finishes, use ‘wait’.
Steps to Execute
1. Create a script file:
nano wait-single.sh
2. Add the following code:
#!/bin/bash
echo "Starting background process..."
sleep 5 &
echo "Waiting for process to complete..."
wait
echo "Process finished."
3. Save and exit (Press ‘Ctrl + O’ (then press ‘Enter’ to confirm the filename), then press ‘Ctrl + X’ to exit).
4. Make the script executable:
chmod +x wait-single.sh
5. Run the script:
./wait-single.sh
Output

Explanation
- ‘sleep 5 &’ runs a background process.
- ‘wait’ pauses script execution until the process completes.
Single Process Bash ‘wait’ Command (Using PID)
Description
Capture the PID of a background task to wait for that specific process instead of all background tasks.
Example Script
1. Create the script:
nano wait-pid.sh
2. Add the code:
#!/bin/bash
sleep 3 &
pid=$!
echo "Waiting for process with PID $pid to finish..."
wait $pid
echo "Specific process with PID $pid is done."
3. Make it executable and run:
chmod +x wait-pid.sh
./wait-pid.sh
Output

Explanation
- ‘$!’ captures the PID of the last background process.
- ‘wait $pid’ pauses until that exact process ends.
Multiple Process ‘wait’ Command
Description
Run several background processes and wait for each one individually by using their PIDs.
Steps to Execute
1. Create a script:
nano wait-multiple.sh
2. Add this code:
#!/bin/bash
echo "Starting multiple processes..."
sleep 3 &
pid1=$!
sleep 2 &
pid2=$!
wait $pid1
echo "Process 1 finished."
wait $pid2
echo "Process 2 finished."
3. Make it executable and run:
chmod +x wait-multiple.sh
./wait-multiple.sh
Output

Explanation
- Two ‘sleep’ commands run in the background.
- ‘wait $pid’ pauses for each one individually.
Multiple Processes Bash ‘wait’ with PID
Description
Waiting on background tasks individually allows precise control over script flow.
Example Script
1. Create a file:
nano multiple-wait-pid.sh
2. Add the following content:
#!/bin/bash
echo "Starting background process 1..."
sleep 3 &
PID1=$!
echo "Starting background process 2..."
sleep 5 &
PID2=$!
echo "Waiting for process 1 to finish (PID: $PID1)..."
wait $PID1
echo "Process 1 finished."
echo "Waiting for process 2 to finish (PID: $PID2)..."
wait $PID2
echo "Process 2 finished."
echo "All background processes are complete."
3. Make it executable and run:
chmod +x multiple-wait-pid.sh
./multiple-wait-pid.sh
Output

Explanation
- Both ‘sleep’ commands launch background processes.
- ‘$!’ captures each PID.
- ‘wait $PID’ pauses for each process to finish.
Multiple Processes Bash ‘wait’ with No PID (Wait All Together)
Description
Use ‘wait’ without any arguments to pause the script until all background jobs complete.
Example Script
1. Create the file:
nano wait-all.sh
2. Add this code:
#!/bin/bash
echo "Starting background tasks..."
sleep 2 &
sleep 4 &
wait
echo "All background jobs finished."
3. Make it executable and run:
chmod +x wait-all.sh
./wait-all.sh
Output

Using ‘wait -n’ to Wait for Any One Job
Description
The ‘-n’ flag causes ‘wait’ to return as soon as any one background job finishes.
Example Script
1. Create a new file:
nano wait-any.sh
2. Add the following code:
#!/bin/bash
sleep 3 &
sleep 5 &
wait -n
echo "At least one background task has finished."
3. Make it executable and run:
chmod +x wait-any.sh
./wait-any.sh
Output

Conclusion
The ‘wait’ command plays a critical role in managing concurrency in Bash scripts. It helps:
- Prevent race conditions
- Synchronize task execution
- Make scripts more reliable
Use it to handle one or many background processes. Whether applying it to PIDs or combining it with the ‘-n’ flag, ‘wait’ offers precise control over script flow and execution timing.
Tip: Combine ‘wait’, ‘$!’, and ‘-n’ to fine-tune background task handling in your Bash automation workflows.
Stop Wasting Time on Servers. Start Building Instead.
You didn’t start your project to babysit servers. Let ServerAvatar handle deployment, monitoring, and backups — so you can focus on growth.
Deploy WordPress, Laravel, N8N, and more in minutes. No DevOps required. No command line. No stress.
Trusted by 10,000+ developers and growing.