Where to start First steps track · 15 stages
Linux Labs

wait — wait for a child task in Bash

4 min Ubuntu 24.04; Bash 5.2 for shell builtins

A Bash builtin that waits for a child task and returns its completion status.

Cheat sheet

6 commands Printable version
  • 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

Overview

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.

Options

3

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.

Recipes

3

Save status before another command

Run this in one shell:

bash
sleep 1 &
child_pid=$!
wait "$child_pid"
child_status=$?
printf 'status=%s\n' "$child_status"

The variable preserves the waiting result even after later commands change $?. In a real script, decide beforehand what should happen when background work fails.

Distinguish suspension from termination

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.

Keep launch and collection together

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.

Troubleshooting

2

The PID is not a child of this shell

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.

The status belongs to an unexpected command

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.

Danger zone

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.

See also

  • background-jobs — background launches and job numbers.
  • processes — parent relationships and zombies.
  • bash — shell execution context.
  • sleep — delaying for a duration.
Loading…