wait
Wait for background processes to complete
TLDR
SYNOPSIS
wait [-fn] [-p varname] [pid|jobspec ...]
DESCRIPTION
wait is a shell builtin that pauses execution until specified background processes complete. It returns the exit status of the waited-for process.When given a PID, wait blocks until that process terminates. With a job specification (%1, %2, etc.), it waits for that specific job. Without arguments, it waits for all child processes.This is essential for scripts that spawn background processes and need to synchronize their completion or check their exit status.The special variable $! contains the PID of the last background process, commonly used with wait: command & pid=$!; wait $pid
PARAMETERS
pid
Process ID to wait for.jobspec
Job specification (e.g., %1, %+, %-).-n
Wait for any single job from the specified list (or all background jobs if none listed) to complete, and return its exit status. Available since Bash 4.3.-f
Force wait for each process to actually terminate before returning, rather than returning when the job status changes (e.g., when stopped). Requires job control to be enabled.-p varname
Assign the PID or job ID of the completed job to the variable varname. Most useful with -n to identify which job finished. Available since Bash 5.1.Without arguments, wait for all background processes.
EXAMPLES
command1 &
command2 &
command3 &
wait # Wait for all three
# Wait and check exit status
long_task &
pid=$!
wait $pid
if [ $? -eq 0 ]; then
echo "Success"
fi
# Process jobs as they complete (Bash 4.3+)
job1 & job2 & job3 &
while wait -n; do
echo "A job completed successfully"
done
# Identify which job finished (Bash 5.1+)
cmd1 & pids+=($!)
cmd2 & pids+=($!)
wait -n -p finished "${pids[@]}"
echo "PID $finished exited with status $?"
CAVEATS
Wait only works with child processes of the current shell. You cannot wait for arbitrary processes started by other shells or users.The -n option (wait for any single job) is a Bash extension not available in all POSIX shells. The -p option requires Bash 5.1 or later.If no arguments are given and no child processes exist, wait returns 0. If a specified PID does not exist, wait returns 127.
