jobs

wait

Blocks until specified background jobs or PIDs complete, returning the exit status of the last one waited for. Essential for scripts that launch parallel background work and need to synchronize.

wait [-n] [-f] [ID ...]

Common flags / operators

Flag / Operator Purpose
(no args) Wait for ALL background children
-n Wait for the next child to finish, return its status (bash 4.3+)
-f Wait for actual termination even if job stopped (bash 5.1+)
PID / %job Wait for a specific PID or job spec

Examples

cmd1 & cmd2 & wait

Fan out two commands in parallel, block until both finish.

cmd & pid=$!; wait "$pid"; echo "status=$?"

Capture background PID via $! then get its exit status.

for u in "${urls[@]}"; do curl -s "$u" & done; wait

Parallel fetch of a URL list.

wait -n; echo "one finished, status=$?"

Return as soon as any one child exits.

Gotcha

Waiting on a PID that already exited returns 127 unless it was a direct child — bash forgets non-child statuses. Under 'set -e', a failing 'wait' exits the script; check status explicitly if you need to tolerate failures.

Related

← All Bash builtins · Linux commands