Beginners Guide for Sleep Command in Linux

Linux TLDR
Last Updated:
Reading time: 3 minutes

As its name suggests, the sleep command is commonly used by shell script writers to delay the execution of individual or portions of commands specified after this command in script.

Using this command, you can delay the next command’s execution for the specified number of seconds (the default), minutes, hours, and days using different suffixes.

In this article, you will learn about the sleep command with its different suffixes and how to use it in shell scripts to pause the execution for a certain period of time.

Tutorial Details

DescriptionDelay the Execution of Script
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitessleep
Internet RequiredNo

Syntax of the Sleep Command

The sleep command takes two arguments: one is the number of x (referring to seconds, minutes, hours, or days based on the suffix), and another is the suffix (the default is second).

$ sleep NUMBER[SUFFIX]

The following is the list of suffixes supported by the sleep command:

  • s” for seconds (default).
  • m” for minutes.
  • h” for hours.
  • d” for days.

By default, when you don’t specify any suffix, the sleep command sets the time interval in seconds. Otherwise, you can choose one or multiple suffixes, as mentioned above.

Note that if you specify multiple suffixes in a single sleep command, the delay before the execution of the next command is equal to the sum of all the times specified in the suffixes.

Different Examples of the Sleep Command

Although you can use this command directly in your terminal, but it is commonly used in shell scripts to delay the execution of the next command.

So, I will show you an example of this command in shell script, but first, read our article on how to run shell script in Linux.

Sleep Command Without Suffix

The following is the basic script where the next “echo 'Done'” command will be delayed for 5s before the execution.

$ cat script.sh 

Output:

Showing the shell script content that will be used for showing the sleep command examples

Note that when you run the above script using the time command, you will find the time it took to execute the script is slightly more than the delay time of 5s.The extra time is just the time your script took to execute.

$ time ./script.sh 

Output:

Running a shell script with sleep command in it to check execution time using time command

Sleep Command with Minute, Hour, and Day Suffix

When working with tasks that require a longer period of time before execution, apart from “second”, you can use the different minute, hour, or day suffixes.

sleep 10m                                                                                                   #Delay for 10 minutes
sleep 10h                                                                                                    #Delay for 10 hours
sleep 1d                                                                                                      #Delay for 1 day

Sleep Command with a Combination of Different Suffixes

While working with the sleep command, single or multiple suffixes can be specified to delay the execution of the next command based on the total number of all the times specified in suffixes.

For example, if you use the following command:

sleep 1d 16h 26m 5s

The script will keep waiting for 1 day, 16 hours, 26 minutes, and 5 seconds to execute the next command.

Splitting Time into Fractions using Floating Integers

The number of seconds can be specified in milliseconds (a fraction of a whole second) using floating point (decimal points) with the sleep command.

For example, the following command will pause the script for 6 milliseconds.

sleep 0.006

Even so, you can use this fraction with other combinations of suffixes.

sleep 1.5h 1.5s

The above command will pause the script for 1 hour, 30 minutes, and 1.5 seconds.

Usage of the Sleep Command in Linux Shell Scripts

From the different examples mentioned in this article, you might have a good idea of this command’s usage. Let’s see how you can implement it in the shell script.

For that, we will use the following shell script, where multiple commands are delayed.

$ cat script.sh 

Output:

Showing the shell script content that will be used for showing the sleep command examples with shell script

In the above script, the “echo 'Sleep for 1.5 minutes'” command will wait for 1.5 minutes, and the “echo 'Sleep for 5.6 seconds'” command will wait for 5.6 seconds.

$ time ./script.sh 

Output:

Running a another shell script with sleep command in it to check execution time using time command

As you can see, the total time it took to execute the script was “1m35.606s“, including the script execution time.

So, that was the last example of this command. I hope you did not go to sleep while reading this article.

But if you intend to go, then don’t forget to subscribe to us and also leave your goodness in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.