How to Use the sleep Command in Bash

Suraj Joshi Feb 02, 2024
  1. Syntax for sleep Command
  2. Example: sleep Command
How to Use the sleep Command in Bash

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.

Syntax for sleep Command

sleep NUMBER [SUFFIX]

Here, NUMBER represents the amount of time to halt the execution of the next command in the script for, and SUFFIX represents the unit of NUMBER. The SUFFIX may take s, m, 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.

Example: sleep Command

sleep 10

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"

Output:

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.

Author: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

Suraj Joshi is a backend software engineer at Matrice.ai.

LinkedIn