sleep command is used to sleep the current process for the specified time. The sleep command delays the execution for a fixed amount of time. The sleep command is very similar to the wait command. The sleep command can be used with the delay amount which can be specified as seconds, minutes, hours, and days. Also using floating-point values can be specified for milliseconds.
sleep Command Syntax
sleep command has very simple syntax like below. It only accepts the sleep value which can be a single parameter or multiple parameters.
- VALUE is the sleep value like 3 seconds or 2 minutes 30 seconds etc. Examples are provided throughout this tutorial.
sleep For Specified Seconds
The most basic usage and defualt usage is sleeping or waiting for the specified number as seconds. If there is no specific specifier the provided value is intrpreter as seconds. In the following example the process sleeps for 5 seconds.
In a more complex case during the execution of the screen the execution is delayed and sleeped for 10 seconds.
#!/bin/bash echo "Current date and time is" date sleep 10 echo "Current date and time is" date
sleep Amount Specifiers
The sleep command can be used with multiple time specifiers like seconds, minutes, hours and days.
Sleep for Specified Miliseconds
The bash execution can be delayed or sleeped for miliseconds by using floating point value for seconds. As 1 secon is equal to the 1000 miliseconds the 500 miliseconds can be expressed as 0.5 second. In the following example we sleep the execution for 200 miliseconds.
Sleep for Specified Minutes
m is used as minutes specifier for the sleep command. In the following example we delay and sleep the execution for 3 minutes.
Sleep for Specified Hours
The hours can be specified with the
h for the sleep command. In the following example we delay and sleep the execution for 1 hour.
Sleep for Specified Days
The days can be specified with the
d for the sleep command. In the following exmaple we delay and sleep the execution for 2 days.
Multiple Sleep Time Specifier
Another useful usage for the sleep command is using multiple time specifiers in a single command to provide in a more human readable way. In the following example we sleep and delay for 1 hour, 13 minutes and 47 seconds.
sleep 1h 13m 47s
Sleep in Multiple Commands
The sleep command can be used between multiple commands to sleep and delay the execution. The sleep command is used between them by chaining them double ampersands or
ls && sleep 2 && mkdir test && sleep 2 && ls
Input/Read Sleep Time From Bash As Input
The sleep command sleep value or time can be read from the bash as an input in an interactive manner. The
read command is used to read the sleep value and set into a variable like
value and then provided to the sleep command as sleep value.
#!/bin/bash echo "Current date and time is" read value sleep $value echo "Current date and time is" date
Display sleep Command Version
Even the sleep command is very mature and simple project and there is no need for new versions with new features we can display the sleep command version with the
--version option like below.
$ sleep --version
Display sleep Command Help
The sleep command help information can be displayed with the
--help option. It provides very basic help information about the time specifiers and version option.
$ sleep --help