In this topic, you will understand how to use sleep command by using different bash scripts. Sleep is a command-line utility which allows us to suspend the calling process for a specified time. In other words, Bash sleep command is used to insert a delay or pause the execution for a specified period of time.
When the programmer needs to pause the execution of any command for the specific purpose, then this command can be used with the specific time value. One can set the delay amount by seconds (s), minutes (m), hours (h), and days (d). This command is especially useful when it is used within a bash shell script.
Sleep Command Syntax
Following is the syntax for the sleep command in Bash:
You can use any positive integer or fractional number as time value. A suffix is an optional part. You can apply any of the following as suffix:
Note: When there is no suffix, then the number is considered to be in seconds (by default).
When two or more arguments are specified, then the total amount of time will be considered as the time equivalent to the sum of their values.
Following are the few simple examples showing how to use sleep command:
Bash Scripts Example
We are going to explain the most basic example of sleep command in Bash.
Bash Console View
How the script works
When we run the script, it will print the current time in HH:MM:SS format. Then the echo command will execute and print the message "wait for 9 seconds". Then the sleep command will execute and pause the script for 9 seconds. When the specified time period elapses, the next line of the script will again print the current time. Lastly, the echo command will print the message "Task Completed".
Similarly, you can run sleep command for minutes, hours, and days.
In this topic, we discussed how we could delay or pause bash execution for a specified amount of time.