Bash wait Command Tutorial

The bash wait command is used to wait for a process that is generally running. The wait command mainly used with the processes ID which can be single or multiple. The wait command generally compared with the sleep. The wait command pauses the given processes until the start trigger but the sleep pause for the specified time and then continues automatically.

wait Command Syntax

The bash wait command has very simple syntax.

  • OPTION is used to set options. This is optional.
  • PID (Process ID) is used to specify the process which is paused. This is required.

The return value of the wait command is the exit status of the specified process. If there are multiple commands to wait the last command exit status is returned.

Wait For A Process

A processes can be wait by using its process ID or PID. In the following example the wait command is used for the process ID 54321.

wait 54321

Wait All Current Active Child Processes

We can also wait for the all child processes which are currently active. There is no parameter or PID specified to the wait command. If we do not provide any option the wait command waits for all currently active child processes by default.


Wait For Multiple Processes

The wait command can be also used to wait multiple processes. All these processes ID’s provided as parameter to the wait command. The multiple processes ID’s delimited with the spaces.

wait 1 2 3 4

Wait For Single Process From Given List of Processes

The wait command can wait for a single processes from the given list of processes. The process list is provided with the processes ID’s. If one of the proces from the given process exits first the wait command exit too and the exited process exit value is returned. The -n option is used to provides processes list or PID.

wait -n 1 2 3 4 5

Leave a Comment