TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
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
id: A Process ID (PID) or a job specification (jobspec, e.g.,%1). If multipleidarguments 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 variablevarname. This is the standard mechanism used to identify exactly which background job terminated when waiting on multiple processes.-f: (Bash 4.4+) Forceswaitto block until the specified job actually terminates, ignoring intermediate job control state changes (such as a process being suspended viaSIGSTOP).
Execution Mechanics
Without Arguments Invokingwait 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).
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.
wait accepts jobspecs managed by the shell’s job control.
Exit Status Propagation
The exit status of thewait command is strictly tied to the processes it monitors:
- If a single
idis specified,waitreturns the exact exit status of that child process. - If multiple
idarguments are specified,waitreturns the exit status of the lastidprovided in the argument list. - If the specified
iddoes not exist, or is not a child process of the current shell,waitimmediately returns an exit status of127. - If
waitis interrupted by a signal, it returns an exit status greater than128.
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.
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.
Master Bash with Deep Grasping Methodology!Learn More





