> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Bash Wait for Job

The `wait` command is a POSIX-compliant shell built-in that suspends the execution of the current shell environment until specified child processes (background jobs) terminate. If a specified process has already terminated prior to the invocation of `wait`, the command does not block; instead, it immediately reaps the cached exit status from the shell's internal job table. Upon resumption or immediate return, it yields the exit status of the awaited process, allowing the parent shell to synchronize with asynchronous execution threads.

## Syntax

```bash theme={"dark"}
wait [-fn] [-p varname] [id ...]
```

* **`id`**: A Process ID (PID) or a job specification (jobspec, e.g., `%1`). If multiple `id` arguments are provided, the shell waits for all specified processes to terminate.
* **`-n`**: (Bash 4.3+) Instructs the shell to wait for the *next* single background job to complete and return its exit status, rather than waiting for a specific job or all jobs.
* **`-p varname`**: (Bash 5.1+) Assigns the PID or jobspec of the process that triggered the return to the variable `varname`. This is the standard mechanism used to identify exactly which background job terminated when waiting on multiple processes.
* **`-f`**: (Bash 4.4+) Forces `wait` to block until the specified job actually terminates, ignoring intermediate job control state changes (such as a process being suspended via `SIGSTOP`).

## Execution Mechanics

**Without Arguments**
Invoking `wait` without any `id` arguments causes the parent shell to block until *all* currently active background processes invoked by that shell have terminated. When used this way, the exit status of `wait` is `0`, unless the command is interrupted by a signal (such as `SIGINT` via `Ctrl+C`), in which case it returns an exit status greater than 128 (e.g., `130`).

```bash theme={"dark"}
command1 &
command2 &
wait
```

**With Specific Arguments**
When provided with one or more `id` arguments, the shell blocks until all specified processes terminate. The command relies on the shell's internal job table to track these processes and retrieve their exit codes, even if they finished before `wait` was called.

```bash theme={"dark"}
command1 &
PID1=$!
command2 &
PID2=$!

wait $PID1 $PID2
```

**Using Job Specifications**
Instead of raw PIDs, `wait` accepts jobspecs managed by the shell's job control.

```bash theme={"dark"}
command1 &
wait %1
```

## Exit Status Propagation

The exit status of the `wait` command is strictly tied to the processes it monitors:

1. If a single `id` is specified, `wait` returns the exact exit status of that child process.
2. If multiple `id` arguments are specified, `wait` returns the exit status of the *last* `id` provided in the argument list.
3. If the specified `id` does not exist, or is not a child process of the current shell, `wait` immediately returns an exit status of `127`.
4. If `wait` is interrupted by a signal, it returns an exit status greater than `128`.

## The `-n` and `-p` Flags Behavior

When using `wait -n`, the shell does not wait for all jobs or a specific job. Instead, it unblocks as soon as *any* single background job terminates. If `wait -n` is provided with specific `id` arguments, it unblocks when the first process among those specific arguments terminates.

Because `-n` returns on the first completed job, the `-p varname` option is required to programmatically determine which process finished.

```bash theme={"dark"}
command1 &
PID1=$!
command2 &
PID2=$!

wait -n -p FINISHED_PID $PID1 $PID2
```

In this execution, `wait` unblocks as soon as either `command1` or `command2` completes. The shell populates the `FINISHED_PID` variable with the exact PID of the terminated process, while the `wait` command itself returns that process's exit status.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Bash Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
