How To Use Bash Sleep In Linux? – POFTUT

How To Use Bash Sleep In Linux?

Sleep is a function provided by bash shell to pause execution of commands. Using sleep function will make delay according to provided time values. There is also library support for sleep function in Linux environments. Simple usage of sleep function is like below.


Syntax of the sleep function is very easy. We will just provide DELAY value to the function.

sleep DELAY
  • DELAY is the time in seconds to wait but also minute hour or days can be specified related suffix which we will examine below.

Delay For 5 Seconds

We will create a simple 5 seconds delay between mkdir and ls command

$ mkdir test && sleep 5 && ls

Here &&  provides sequential execution.

Delay For 5 Seconds
Delay For 5 Seconds

We can also use this expression in a script line by line like below

sleep 5

By default is given delay parameter to the sleep, the function is interpreted as seconds but we can change the interpretation by providing.

Sleep Specified Minutes

We can provide a delay parameter as minute by providing mpostfix. In this example, we will create a 1-minute delay

$ sleep 1m

Sleep Specified Hours

Hours delay can be specified like below. We will use h as a prefix. The following example will wait for 1 hour before executing the next command.

$ sleep 1h

Sleep Specified Days

And the last day can be specified like below. We will use d as the prefix to specify the day. Following the example, the command will wait for 1 day.

$ sleep 1d

Multiple Duration Expressions

Up to now, we have looked single duration specification but sleep supports multiple duration specification. We can specify multiple values for sleep like 1 day 5 hours like below.

$ sleep 1d 5h

Sleep Examples

sleep .5 # Waits 0.5 second.
sleep 5 # Waits 5 seconds.
sleep 5s # Waits 5 seconds.
sleep 5m # Waits 5 minutes.
sleep 5h # Waits 5 hours.
sleep 5d # Waits 5 days.

Example Sleep Script

This is an example script that can be used for different purposes. Create filename and add the following lines.

echo "I will sleep for 10 seconds"; 
sleep 10; 
echo "I waked up after sleeping 10 seconds";

LEARN MORE  Linux Bashrc File and Usage Examples

1 thought on “How To Use Bash Sleep In Linux?”

Leave a Comment