ServerAvatar Logo

How to Use the wait Command in Bash

  • Author: Meghna Meghwani
  • Published: 18 August 2025
  • Last Updated: 19 August 2025
How to Use the wait Command in Bash

Table Of Contents

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:

Bash
wait [pid ...]
OptionDescription
waitWaits for all background jobs to complete.
wait PIDWaits 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:

Bash
nano wait-single.sh

2. Add the following code:

Bash
#!/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:

Bash
chmod +x wait-single.sh  

5. Run the script:

Bash
./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:

Bash
nano wait-pid.sh

2. Add the code:

Bash
#!/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:

Bash
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:

Bash
nano wait-multiple.sh

2. Add this code:

Bash
#!/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:

Bash
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:

Bash
nano multiple-wait-pid.sh

2. Add the following content:

Bash
#!/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:

Bash
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:

Bash
nano wait-all.sh

2. Add this code:

Bash
#!/bin/bash   
echo "Starting background tasks..."   
sleep 2 &   
sleep 4 &   
wait   
echo "All background jobs finished."

3. Make it executable and run:

Bash
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:

Bash
nano wait-any.sh  

2. Add the following code:

Bash
#!/bin/bash   
sleep 3 &   
sleep 5 &   
wait -n   
echo "At least one background task has finished."

3. Make it executable and run:

Bash
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.

Deploy your first application in 10 minutes, Risk Free!

Learn how ServerAvatar simplifies server management with intuitive dashboards and automated processes.
  • No CC Info Required
  • Free 4-Days Trial
  • Deploy in Next 10 Minutes!