How To Use The Linux sleep Command To Pause A BASH Script

Linux Sleep Command
Linux Sleep Command.


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

On its own, the sleep command is completely useless unless you like locking up your terminal window but as part of a script it can be used in many different ways including as a pause factor before retrying a command.

For instance, imagine you had a script that processed files copied 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.

The script for copying the files may well contain a loop to test whether all of the files have been downloaded (i.e. it knows there should be 50 files and when 50 files have been found the copy process is started).

There is no point the script continually testing as it takes up processor time. Instead, you might choose to test whether there are enough files copied and if there aren't pause for a few minutes and then try again. The sleep command is perfect in these 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 you to the command line.

The sleep command requires the keyword sleep followed by the number you wish to pause by and then 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 waiting days for something to happen it might be 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 on end.

The number for the sleep command 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 Use For 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 ))


The script sets the variable x to 10. The while loop will continue to iterate whilst 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" (i.e. 10) and then subtracts 1 from the value of x.

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

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 command shows the version of the sleep command that is installed on your system.

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

  • version number
  • copyright details
  • License
  • Authors

    Further Reading

    The following links highlight other Linux commands you might wish to know about: