wait Command
Intermediate Shell Scripting man(1)Wait for background processes to finish
๐ 105 views
๐
Updated: Apr 29, 2026
SYNTAX
wait [PID]...
What Does wait Do?
wait pauses the shell until specified background processes complete. Without arguments, it waits for all background jobs. With a PID or job ID, it waits for that specific process.
wait returns the exit status of the waited-for process, making it essential for parallel processing scripts that need to check if background tasks succeeded or failed.
wait is commonly used with background (&) processes, parallel command execution, and coordinating multiple asynchronous operations in shell scripts.
wait returns the exit status of the waited-for process, making it essential for parallel processing scripts that need to check if background tasks succeeded or failed.
wait is commonly used with background (&) processes, parallel command execution, and coordinating multiple asynchronous operations in shell scripts.
Options & Flags
| Option | Description | Example |
|---|---|---|
| PID | Wait for specific process | wait 1234 |
| %N | Wait for job number N | wait %1 |
| -n | Wait for any single job to complete (bash 4.3+) | wait -n |
| -p VAR | Store the PID that completed (bash 5.1+) | wait -n -p completed_pid |
| (no args) | Wait for all background jobs | wait |
Practical Examples
#1 Wait for all background jobs
Runs 3 commands in parallel and waits for all to finish.
$ cmd1 & cmd2 & cmd3 & wait; echo "All done"#2 Wait for specific process
Starts a task, captures its PID, waits for it, and checks its exit status.
$ long_task & PID=$!; echo "Started $PID"; wait $PID; echo "Exit code: $?"#3 Parallel with error checking
Runs tasks in parallel and checks each exit code independently.
$ task1 & P1=$!; task2 & P2=$!; wait $P1 || echo "task1 failed"; wait $P2 || echo "task2 failed"#4 Wait for any job
Starts multiple jobs and continues as soon as any one finishes.
$ for i in 1 2 3 4 5; do slow_task $i & done; wait -n; echo "First one done"#5 Parallel file processing
Processes all CSV files in parallel, then continues.
$ for f in *.csv; do process "$f" & done; wait; echo "All files processed"Tips & Best Practices
$! captures last background PID: After command &, $! contains its PID. Save it immediately: cmd & PID=$! for use with wait later.
Exit code propagation: wait returns the exit code of the process it waited for. Use $? after wait to check if the background task succeeded.
Only waits for children: wait can only wait for processes started by the current shell. It cannot wait for arbitrary PIDs from other sessions.
Frequently Asked Questions
How do I wait for background processes?
Use wait with no arguments to wait for all background jobs, or wait PID for a specific process.
How do I get the exit code of a background process?
cmd & PID=$!; wait $PID; echo $?. The $? after wait gives the exit code of the background process.
How do I run commands in parallel and wait?
Start each with &, then call wait: cmd1 & cmd2 & cmd3 & wait. This waits for all three to complete.
Related Commands
More Shell Scripting Commands
Download Shell Scripting Cheat Sheet
View all 31 Linux command cheat sheets โMaster Linux with Professional eBooks
Curated IT eBooks covering Linux, DevOps, Cloud, and more
Browse Books โ