Bash Sleep Command - Pause the execution of a bash script

The bash shell provides an easy way for us to pause the execution of a shell script through the sleep command.

The sleep command takes a number as its parameter and then pauses the execution of the shell script for that number of seconds.

This tutorial has bash sleep examples to get you up to speed with using the bash sleep command.

Why would we want to pause the execution of a script?

Making bash sleep for a specified time may be useful in a situation where we are performing hardware intensive tasks and we don't want to overuse the resources of the system. It could also be useful in a situation where we are requesting a service from a slower machine and we want to give it time to perform its job.

Even more simply than that, we could use it between printing messages to the screen to give the user enough time to read the messages before moving on.

Bash Sleep Command Example

The simple example below will output two messages (separated by a pause) to the console screen. If we wanted to output a string in our console, wait 5 seconds, and then output another string, we would do it like this:

echo "First thing's first..."
sleep 5
echo "We just waited 5 seconds"

If we added the above code to a shell script and then ran that script, we would see that the first message gets printed to the screen, then we would have a 5 second pause, and then the second message would be printed to the screen.

That shows basically how the bash sleep command works. Of course, you would use it as necessary (and with the appropriate delay) when writing your own shell scripts. If you plan to use delays between writing messages to the screen, take care that enough delay is given so that the user can comfortably read each message.

We hope this tutorial on the bash sleep command has been helpful.

Thank Tutorial Arena for This Tutorial.
Show your appreciation with a +1...