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

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

What To Know

  • Use the sleep command plus a time; s=seconds, m=minutes, h=hours, or d=days (for example, sleep 5s pauses the script for 5 seconds).
  • Use man sleep for more.

This article explains how to use the Linux sleep command to pause a bash script, among other things. On its own, the sleep command isn't very useful. However, as part of a script, it can be used in many ways. For example, you can use it to pause the script before retrying a command that failed the first time.

Person running Linux sleep command for 20 seconds on a laptop
Kaley McKean / Lifewire

An Example of Using the Sleep Command

Imagine you have a script that processes files that were downloaded from another server. The script shouldn't start the copy process until all the files finish downloading. The download process is performed by a separate script that runs before yours.

The script that copies the files may contain a loop to test whether all the files have downloaded (it does this by checking whether 50 files are found before starting the copy process).

There's no point in the script testing constantly since this uses 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 makes the terminal pause for 5 seconds before returning to the command line.

The sleep command requires the keyword sleep, followed by the number you want 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, use a cron job to run the script at regular intervals, as opposed to having a script run 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. These are 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.

Linux sleep command

For example, the following syntax includes a fraction of a second:

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 ))

Here's how this script works:

  • The script sets the variable x to 10.
  • The while loop continues to iterate while the value of x is greater than zero.
  • The sleep command pauses the script for 1 second each time around the loop.
  • 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.
Linux sleep in script

Without the sleep command, the script would zoom through, and the messages would display 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 the system.

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

  • Version number
  • Copyright details
  • License
  • Authors

Pause Terminal Commands with Sleep

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

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

However, a faster approach is to type the two commands on one line, with a sleep command between each command:

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

How this command works:

  • The cd /mydirectory/ command changes the directory.
  • The sleep 3 command waits three seconds for the cd command to finish.
  • The ls command executes and displays the directory contents.

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

Was this page helpful?