This article explains the sleep command in Bash/Shell scripts and how and why you might use it.
The sleep command in Bash (and other Linux Shells) pauses execution for a specified amount of time – halting the script for a set number of seconds, minutes, hours, etc.
Why Pause Execution?
Why would you want to pause executing your script?
- Give the user a chance to interrupt an automated action
- Await user input
- Wait for a device to warm up/become available
- Stop text from flying across the screen if you’re trying to read some script output
The syntax for the sleep command is as follows:
- NUMBER is the units of time you want the script to pause for (defaults to seconds)
- The number can be a decimal; it doesn’t have to be a whole integer
- UNITS is the unit of time you wish to sleep for.
- Defaults to seconds
- Can be one of either s (seconds), m (minutes), h(hours), or d (days)
Example Bash Script for sleep
#!/bin/bash echo "Hello there!" sleep 6 echo "It's been 6 seconds since I said 'Hello there!'" sleep 7m echo "It's been 7 minutes and 6 seconds since I said 'Hello there!'" sleep .5 echo "It's been 7 minutes and 6.5 seconds since I said 'Hello there!'"