BASH Programming

Bash-Difference Between Wait and Sleep

Bash is a powerful shell scripting language used in Unix-based operating systems. The two of the most used commands in Bash are wait and sleep and both commands are used to pause the execution of a Bash script, but they work differently.

This article will explore the differences between wait and sleep commands in Bash and provide examples for each command.

Sleep Command

The sleep command is used to pause the execution of a script for a specified number of seconds and is often used to introduce the delays between commands or to pause a script before continuing execution, below is the syntax for the sleep command:

sleep <seconds>

Where seconds is the number of seconds to pause the script, for further illustration I have given an example bash code that just pauses the code execution for 5 seconds, here is an example:

#!/bin/bash
echo "Starting a long-running process."
# Pause for 5 seconds
sleep 5
echo "Continuing with the script."

The code prints a message to indicate that we are starting a long-running process and then uses the sleep command to pause the script for 5 seconds before continuing with the next command and once the sleep time is over the script will continue its execution:

Wait Command

The wait command is used to pause the execution of a script until all child processes have completed. It is often used in conjunction with the & operator, which allows a script to execute multiple commands simultaneously, below is the syntax for the wait command

wait

For further illustration I have given an example bash code that just waits to execute the all the processes running in background using the wait command and once all the processes are completed it prints a message of task completed:

#!/bin/bash

# Define a function that performs a time-consuming task
function time_consuming_task {
    echo "Starting time-consuming task $1"
    sleep $2
    echo "Completed time-consuming task $1"
}

# Start multiple time-consuming tasks in the background
time_consuming_task "Task 1" 5 &
time_consuming_task "Task 2" 3 &
time_consuming_task "Task 3" 7 &

# Wait for all background tasks to complete
wait

# Print a message indicating that all tasks have completed
echo "All time-consuming tasks have been completed."

Here we have defined a function called time_consuming_task that simulates a time-consuming task by using the sleep command to pause the execution of the script for a specified amount of time. We then start multiple instances of this function in the background using the & symbol.

After starting the background tasks, we use the wait command to pause the execution of the script until all background tasks are completed. Finally, we print a message indicating that all time-consuming tasks have been completed:

This is how the wait command can be used to control the execution of background tasks in Bash. By using the wait command, we can ensure that our script waits for all background tasks to finish before continuing with the next command. This can be particularly useful when we need to perform multiple time-consuming tasks in parallel and need to wait for all tasks to complete before moving on to the next step

Difference Between Wait and Sleep

The key difference between wait and sleep is that wait is used to pause a script until all child processes have completed, while sleep is used to pause a script for a specified number of seconds. The wait is typically used in conjunction with the & operator to allow a script to execute multiple commands simultaneously, while sleep is used to introduce delays between commands or to pause a script before continuing execution.

Conclusion

The wait and sleep are two commonly used commands in Bash that are used to pause the execution of a script. The wait is used to pause a script until all child processes have completed, while sleep is used to pause a script for a specified number of seconds. By understanding the differences between these commands, you can use them effectively in your Bash scripts to improve efficiency and automate tasks.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.