How to Use the Linux Sleep Command to Pause a BASH Script

Want to slow down your BASH script? Use the sleep command

boy sleeping on floor with dog
Miguel Sanz/Getty Images

This guide shows how to use the Linux sleep command to pause a bash script.

On its own, the sleep command isn't very useful, but as part of a script, it can be used in many different ways. For example, you can use it to pause the script before retrying a command that failed the first time.

An Example of Using the Sleep Command

Imagine you had a script that processes files downloaded from another server. The script should not start the copy process until all of the files have finished downloading. The download process is performed by a completely separate script that runs before yours.

Your script for copying the files may contain a loop to test whether all of the files have been downloaded (it does this by checking whether 50 files are found before starting the copy process).

There is no point of the script testing constantly since this would eat up processor time. Instead, you might pause for a few minutes between each test before trying again. The sleep command is perfect in such circumstances.

How to Use the Sleep Command

To use the Linux sleep command, enter the following into the terminal window:

sleep 5s

The above command will make your terminal pause for 5 seconds before returning to the command line.

The sleep command requires the keyword 'sleep' followed by the number you wish to pause and the unit of measure. 

You can specify the delay in seconds, minutes, hours, or days.

  • s - seconds
  • m - minutes
  • h - hours
  • d - days

When it comes to pausing a script for days, it's worth considering using a cron job to run the script at regular intervals, as opposed to having a script running in the background for days.

A cron job is a Linux command or script that you can schedule to run at a set time or day. They're very useful for repeating tasks over a long period of time.

The number for the sleep command interval doesn't have to be a whole number. You can also use floating point numbers.

For example, it is perfectly ok to use the following syntax:

sleep 3.5s

An Example of Using the Sleep Command

The following script shows how to use the sleep command to make a terminal based countdown clock:

while [ $x -gt 0 ]
sleep 1s
echo "$x seconds until blast off"
x=$(( $x - 1 ))

How this script works:

  1. First, this script sets the variable x to 10.

  2. The while loop continues to iterate while the value of x is greater than zero.

  3. The sleep command pauses the script for 1 second each time around the loop.

  4. The rest of the script clears the screen each iteration, displays the message "x seconds until blast off", and subtracts 1 from the value of x.

Without the sleep command, the script would zoom through and the messages would be displayed too quickly.

How to Use Sleep Command Switches

The sleep command only has a couple of switches.

The --help switch shows the help file for the sleep command. You can achieve the same thing by using the man command as follows:

man sleep

The --version switch shows the version of the sleep command that's installed on your system.

The information returned by the --version switch is as follows:

  • Version number
  • Copyright details
  • License
  • Authors

Pausing Terminal Commands with Sleep

Another good use for the sleep command is to pause commands that you type in the terminal window.

If you wanted to, you could type two commands in a row, waiting for the first one to finish before typing the second.

However, a faster approach is typing the two commands in one line, with a sleep command between them

$ cd /mydirectory/ && sleep 3 && ls

How this command works:

  1. The cd /mydirectory/ command will change the directory.
  2. The sleep 3 command will wait three seconds for the cd command to finish.
  3. The ls command will then execute and display all of the directory contents.

For a simple example like this, the sleep command only saves a little bit of time. But if you have a long list of commands, the ability to type them all on one line is a real time saver.