Save status before another command
Run this in one shell:
The variable preserves the waiting result even after later commands change
$?. In a real script, decide beforehand what should happen when background
work fails.
A Bash builtin that waits for a child task and returns its completion status.
type wait
confirm that this is a builtin
help wait
current Bash help
sleep 2 &
create a short child task
child_pid=$!
save its PID in this shell
wait "$child_pid"
wait for its result
printf '%s\n' "$?"
immediately display wait status
wait is a shell builtin for waiting for that shell's child tasks. It is not
a universal way to wait for any process found with ps. A
job table belongs to a particular shell.
A background launch returns the prompt before the program's result is
available. wait connects those stages: start work concurrently, then obtain
its outcome. Save the PID immediately after the corresponding launch.
Bash 5.2 supports waiting for a PID or job, -n for the next completed
eligible child, and -p to save the selected identifier together with -n.
With job control enabled, -f requires termination rather than merely a state
change.
Without operands, the command waits for the shell's background tasks and
returns zero. That is not an aggregate guarantee that every task succeeded:
collect individual statuses for that. Use help wait for the installed
version's details.
| Option | What it does | Example |
|---|---|---|
-n |
Wait for the next one of the listed child processes to finish. | wait -n "$pid1" "$pid2" |
-p var |
Together with -n, store the finished process's PID in a variable. | wait -n -p done_pid "$pid1" "$pid2" |
-f |
With job control, wait for actual termination, not just a stopped state. | wait -f "$child_pid" |
Nothing found. Try a shorter query.
Run this in one shell:
The variable preserves the waiting result even after later commands change
$?. In a real script, decide beforehand what should happen when background
work fails.
For your saved PID, use wait -f "$child_pid" when job control is enabled and
termination is required. A suspended task can still resume; state T is not a
completed result.
Start the task and wait for it in the same shell. Copying a PID number into another terminal does not transfer parent relationships or the job table.
Check where the task was started. For an arbitrary service, use its manager and a result check. Choosing a different PID does not repair missing parenthood.
Another command ran between waiting and reading $?. Save the value
immediately, as in the recipe above. A returned prompt alone does not tell you
whether the task succeeded.
Waiting may take a long time if the child does not finish. wait neither
requests termination nor sets a timeout.
In scripts using set -e, a nonzero status needs deliberate handling;
otherwise execution may stop before the result is examined. This article does
not replace the error-handling policy of the whole script.
Do not confuse the Bash builtin with the wait/waitpid system calls that programs use to collect their children's statuses.
Found an inaccuracy? Report an error in the article