Bash wait Command | Explained

In Linux, the wait command is used to pause the execution of a script until a specified process completes. This can be useful in several situations, such as running multiple commands in a sequence or ensuring that a certain command or process has finished before continuing with the script.

This post will address the working and usage of the wait command in Linux with the following outcomes:

Let’s begin with a basic understanding of the wait command.

What is a wait Command in Linux?

The wait command in Linux is used for the previous commands to run and execute the next commands.

The general syntax of the wait command is given below:

$ wait [option] PID
  • wait: The wait word is used to specify the wait command.
  • [option]: Replace it with the preferred option for the wait command.
  • PID: Put specified Process ID or Job specification to wait.

The wait command is used in several ways, which are as follows:

waitAfter all background processes are completed, it executes the next commands.
wait [PID or Job Spec]It waits for the specified PID or Job ID mentioned with the wait command.
wait -f [PID1 PID2 …]It waits for the execution of all the PIDs and then terminates the process with an exit code.
wait -n [PID1 PID2 …]wait for only a single process to complete, then return the exit code.

Let’s check the uses of the wait command with examples.

How to Use a wait Command in Bash?

The wait command is used for a single process, multiple processes, and the last or the first process. Let’s use this wait command with examples.

Example 1: Bash wait Command for a Single Process

A single process can be paused with the wait command to execute that process. To wait for a process, check the bash script code below:

Note: The & at the end of the line shows the process is running in the background.

#!/bin/bash

echo "Linux" &
echo "Ubuntu"
wait
echo "Fedora"

Let’s understand the above bash script line-by-line:

  • The #!/bin/bash tells the system that the bash shell interpreter is used.
  • First echo command is used to run the background process.
  • Second echo command runs the process in the foreground.
  • wait: It will wait for background and foreground processes to complete and execute the next echo command.
  • The third echo will execute once the above processes are complete.

To allow the execute permissions for the script, run the below command:

Note: The execution must be provided to run the bash script.

$ chmod u+x wait.sh

Now, run the script by writing the script name in the terminal as shown below:

$ ~/wait.sh

The output shows that it waits for all the previous processes to complete (including the background process) and then executes the next command.

Example 2: Bash wait Command for Multiple Processes

Several wait commands are used to wait for multiple processes to execute before running the next commands. For instance, use the below script with wait commands to complete the previous process first, then run the next commands:

#!/bin/bash

echo "Linux" &
echo $PWD
wait
sleep 5s
wait
echo $HOSTNAME

The above bash script will work this way:

  • Prints the echo command in the background (which will execute later than the foreground process before waiting) and prints the $PWD variable output.
  • wait for the above two commands to complete the execution.
  • wait for 5 seconds for the sleep command to execute.
  • After completing all the above processes, the last echo command, which prints the hostname, executes.

Let’s run the bash script using the below command:

$ bash wait.sh

The output shows the foreground output waits for the background process to complete. After completing previous processes, it sleeps for 5 seconds and displays the user’s hostname (itslinuxfoss).

Example 3: Bash wait Command for a PID

We can use the wait command to execute a specific process only using its PID. The PID=$! is used to get the process ID (PID) of the last executed process. The below script is used to wait for the process using its PID:

#!/bin/bash

echo "Linux" &
PID=$!
wait $PID
echo $PWD
wait
echo $HOSTNAME

Let’s discuss how the above script will work:

  • The first wait command waits for the last executed process ID (which is for the background process echo command).
  • After that PWD command executes, and the wait command pauses the system to execute the above commands completely.
  • In the end, the HOSTNAME command is executed.

The following command will run the script:

$ ~/wait.sh

The output shows the background echo command output, which waits for the echo process PID after it moves to the next $PWD command, and in the end, it executes the $HOSTNAME command.

Example 4: Bash wait Command Using Options

The “f” option of the wait command waits for all the previous processes to complete before running the next command. While the “n” option only waits for a single process (first process) and executes the next commands.

Let’s create a script with the “f” option, which waits for all the processes as shown below:

#!/bin/bash

echo "Linux" & 
echo $PWD 
echo "Ubuntu" & 
wait -f 
echo $HOSTNAME

To check the bash script output, execute this command:

$ ~/wait.sh

The output shows that all the commands before the wait command are executed first, then the next commands are executed.

To check the working of the n option, which only waits for the first process, use the below bash script:

#!/bin/bash

echo "Linux" &
echo $PWD
echo "Ubuntu" &
wait -n
echo $HOSTNAME

Let’s execute the script with this command:

$ ~/wait.sh

The output verifies that the “wait -n” command only waits for the first command (in this case, “echo “Linux” &”) after that, it executes the HOSTNAME command, and in this last, it executes “echo “Ubuntu” &” command which was executed before for the “wait -f” command.

Conclusion

The wait command in the Linux bash scripts waits for the previous commands to complete execution, and after that, it executes the next commands. The wait command is used to wait to complete execution for a specific PID, for the first PID only, and all the PIDs, as discussed in this tutorial.