Bash Sleep Command

Bash Sleep command is used to insert a delay or pause the execution for a specified period of time.

Following is the syntax of bash sleep :

</>
Copy
 sleep NUMBER[SUFFIX]

SUFFIX is optional and can be :

  • s – seconds
  • m – minutes
  • h – hours
  • d – days

When no SUFFIX is provided, by default, the NUMBER is considered to be seconds.

Some of the examples are :

  • sleep 45s
  • sleep 2m
  • sleep 9h
  • sleep 2d

Examples for Bash Sleep Command

Sleep in Seconds

Following is an example bash script to sleep or delay by 4 seconds.

Bash Script File

</>
Copy
#!/bin/bash

echo HH:MM:SS

echo `date +%H:%M:%S/%m-%d-%Y`;
sleep 4
echo `date +%H:%M:%S/%m-%d-%Y`;

Output

HH:MM:SS
10:56:09
10:56:13

You can also specify that the units as seconds (s).

Bash Script File

</>
Copy
#!/bin/bash

echo HH:MM:SS

echo `date +%H:%M:%S`;
sleep 3s
echo `date +%H:%M:%S`;

Output

HH:MM:SS
10:55:11
10:55:14

Sleep in Minutes

Following bash script demonstrates to delay bash execution in minutes.

Bash Script File

</>
Copy
#!/bin/bash

echo HH:MM:SS

echo `date +%H:%M:%S`;
sleep 2m
echo `date +%H:%M:%S`;

Output

HH:MM:SS
10:56:10
10:58:10

Similarly sleep command can be run for hours and days.

Conclusion

In this Bash Tutorial, we have learnt to delay or pause bash execution by a specified amount of time.