sleep command in Bash halts the next command’s execution for the specified amount of time. This command becomes handy when we want to check a certain status repetitively until the status becomes what we want.
sleep NUMBER [SUFFIX]
NUMBER represents the amount of time to halt the execution of the next command in the script for, and
SUFFIX represents the unit of
SUFFIX may take
h, and as its values represent seconds, minutes, hours, and days respectively. The default value of
SUFFIX represents seconds. If more than one argument is specified, the execution will halt for the time equivalent to the sum of all the argument values.
It halts the execution of the next command in the script for 10 seconds.
sleep 5m 50s
It halts the execution of the next command in the script for 5 minutes and 50 seconds.
#!/bin/bash echo "Time Before Sleep Statement:" date +"%H:%M:%S" sleep 3 echo "Time After Sleep Statement:" date +"%H:%M:%S"
Time Before Sleep Statement: 20:12:15 Time After Sleep Statement: 20:12:18
It prints the time before and after executing the
sleep statement. From the output, it is clear that the
sleep command halts the program’s execution for 3 seconds.