sleep Command in Linux: Pause a Bash Script

The sleep command in bash pauses the execution of a script for a specified amount of time. It is one of the most common commands used in shell scripting, especially when you want to retry a failed operation, rate-limit a series of requests, or add a small delay inside a loop.
In this guide, we will show you how to use the sleep command with practical examples, including how to sleep for fractional seconds, how to combine multiple time units, and how to run sleep in the background.
Syntax
The syntax for the sleep command is as follows:
sleep NUMBER[SUFFIX]...The NUMBER may be a positive integer or a floating-point number.
The SUFFIX may be one of the following:
s- seconds (default)m- minutesh- hoursd- days
When no suffix is specified, it defaults to seconds.
When two or more arguments are given, the total amount of time is equivalent to the sum of their values.
Sleep Command Examples
The following examples show the basic forms of the sleep command.
Sleep for Seconds
To make a script wait for a given number of seconds, pass the number without a suffix. The command below delays execution for 5 seconds:
sleep 5Sleep for Fractional Seconds
For delays shorter than a second, pass a decimal value:
sleep 0.5Sleep for Multiple Durations
You can combine multiple values. The following pauses for 2 minutes and 30 seconds:
sleep 2m 30sSleep Indefinitely
To pause a process indefinitely until it is manually interrupted, use infinity:
sleep infinityThis is commonly used in Docker containers and systemd services to keep a process running without consuming CPU.
Script Examples
The following examples show how sleep is commonly used in bash scripts.
Timing a Delay
This script prints the time before and after a 5-second delay:
#!/bin/bash
# start time
date +"%H:%M:%S"
# sleep for 5 seconds
sleep 5
# end time
date +"%H:%M:%S"When you run the script, it will print the current time
in HH:MM:SS format. Then the sleep command pauses the script for 5 seconds. Once the specified time period elapses, the last line prints the current time.
13:34:40
13:34:45Waiting for a Host to Come Online
The following script checks whether a host is online every 5 seconds and notifies you when it becomes reachable:
#!/bin/bash
while :
do
if ping -c 1 192.168.1.10 &> /dev/null
then
echo "Host is online"
break
fi
sleep 5
doneHow the script works:
- The
whileloop runs indefinitely using:as the condition. - The
pingcommand sends a single packet to the target host. - If the host is reachable, the script prints “Host is online” and exits the loop with
break. - If the host is not reachable,
sleep 5pauses the script for 5 seconds before retrying.
Retry with Exponential Backoff
When retrying a failed operation, it is good practice to increase the delay between attempts. This avoids overwhelming the target service:
#!/bin/bash
max_retries=5
delay=1
for ((i = 1; i <= max_retries; i++)); do
if curl -s --fail https://example.com > /dev/null; then
echo "Request succeeded on attempt $i"
exit 0
fi
echo "Attempt $i failed. Retrying in ${delay}s..."
sleep "$delay"
delay=$((delay * 2))
done
echo "All $max_retries attempts failed."
exit 1The delay starts at 1 second and doubles after each failure: 1s, 2s, 4s, 8s, 16s.
Countdown Timer
A simple countdown timer that prints the remaining seconds:
#!/bin/bash
seconds=${1:-10}
for ((i = seconds; i > 0; i--)); do
printf "\rTime remaining: %d seconds " "$i"
sleep 1
done
printf "\rTime's up! \n"Run the script with an optional number of seconds:
bash countdown.sh 30Running Sleep in the Background
You can run sleep in the background to create non-blocking delays:
sleep 10 &
echo "Sleep is running in the background with PID $!"The & operator sends the process to the background. The special variable $! holds the PID of the last background process.
To wait for a background sleep to finish, use the wait command
:
sleep 10 &
pid=$!
echo "Doing other work..."
wait "$pid"
echo "Sleep finished"Pausing Until a Keypress
In some scripts, you may want to pause until the user presses a key instead of waiting for a fixed amount of time. The sleep command cannot do this on its own, but you can combine it with the read command
:
#!/bin/bash
read -n 1 -s -r -p "Press any key to continue..."
echoThe -n 1 option tells read to accept a single character, -s hides the keypress from the terminal, and -r prevents backslash interpretation. This is a common way to make a script wait for confirmation before continuing.
To pause until a keypress but continue on its own if nobody is watching, add the -t option:
#!/bin/bash
read -t 10 -n 1 -s -r -p "Press any key to continue (continuing in 10s)..."
echoThe script waits up to 10 seconds for a keypress and then moves on. The same option works when reading a value, so read -t 5 -p "Enter your name: " name gives the user 5 seconds to type a name before the script continues without one.
Interrupting Sleep
To interrupt a running sleep command, press Ctrl+C. This sends a SIGINT signal that terminates the process.
To terminate a background sleep process, use kill:
sleep 300 &
kill $!Quick Reference
| Task | Command |
|---|---|
| Sleep for 5 seconds | sleep 5 |
| Sleep for 0.5 seconds | sleep 0.5 |
| Sleep for 2 minutes | sleep 2m |
| Sleep for 1 hour 30 minutes | sleep 1h 30m |
| Sleep indefinitely | sleep infinity |
| Sleep in background | sleep 10 & |
| Interrupt sleep | Ctrl+C or kill PID |
FAQ
Is sleep a bash builtin?
No. The sleep command is provided by the GNU coreutils package and lives at /usr/bin/sleep. Because it is an external program rather than a shell builtin, the GNU version supports features like fractional seconds and the infinity argument that are not part of the POSIX specification.
Can sleep accept decimal values?
Yes. The GNU version of sleep used on Linux supports floating-point numbers. For example, sleep 0.5 pauses for half a second. The POSIX specification only requires integer support, so this will not work on every Unix system.
Why does sleep report “invalid time interval”?
The argument is not a number followed by an optional unit. The usual causes are a space between the number and its suffix (sleep 2 m instead of sleep 2m), a unit the command does not support such as w for weeks, or a variable that expanded to an empty string. Quote the variable and print its value before passing it to sleep.
What is sleep infinity used for?sleep infinity pauses a process indefinitely without consuming CPU. It is commonly used in Docker containers to keep the container running, or in scripts that need to wait for a signal.
Does sleep use CPU while waiting?
No. The sleep command suspends the process and does not consume CPU cycles during the wait period.
How do I cancel a sleep in a script?
Press Ctrl+C to send SIGINT to the foreground process. For background sleep processes, use kill PID where PID is the process ID returned by $!.
Conclusion
The sleep command pauses the execution of the next command for a given amount of time. It is commonly used in shell scripts for retrying operations, rate limiting, and scheduling delays.
For longer scripts, keep delays explicit and pair them with clear output so you can tell whether the script is waiting, retrying, or finished.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page