Bash Sleep Command
The Bash sleep command pauses a shell script or terminal command for a specified amount of time. It is commonly used to delay the next command, space out repeated operations, wait before retrying a task, or reduce the frequency of a loop.
sleep pauses the current process for at least the requested duration. The actual delay may be slightly longer because of system scheduling and workload.
Bash Sleep Command Syntax
Following is the syntax of bash sleep :
sleep NUMBER[SUFFIX]
NUMBER specifies how long the command should pause. SUFFIX is optional and can be:
s– secondsm– minutesh– hoursd– 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
The suffix is written immediately after the number without a space. For example, use sleep 10m, not sleep 10 m.
Common Bash Sleep Command Examples
| Command | Pause duration |
|---|---|
sleep 1 | 1 second |
sleep 5s | 5 seconds |
sleep 1m | 1 minute |
sleep 10m | 10 minutes |
sleep 2h | 2 hours |
sleep 1d | 1 day |
sleep 0.5 | Half a second on implementations that support fractional values |
Examples for Bash Sleep Command
Sleep in Seconds
Following is an example bash script to sleep or delay by 4 seconds.
Bash Script File
#!/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
The second timestamp appears approximately four seconds after the first one.
You can also specify that the units as seconds (s).
Bash Script File
#!/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
#!/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.
Bash Sleep for Milliseconds and Fractional Seconds
GNU sleep and several other common implementations accept fractional values. This makes it possible to pause for less than one second.
sleep 0.5
sleep 0.25s
sleep 0.001
These commands request delays of 500 milliseconds, 250 milliseconds, and 1 millisecond respectively. Fractional-duration support can vary between operating systems and sleep implementations, so check the local manual when writing a portable script.
To verify the version and supported syntax on a GNU/Linux system, run:
sleep --version
man sleep
Using Bash Sleep Between Commands
Place sleep between commands when the next operation must not begin immediately.
echo "Starting task"
sleep 5
echo "Continuing after five seconds"
The shell runs the first echo, pauses for five seconds, and then executes the final command.
Using Bash Sleep in a Loop
A sleep interval inside a loop prevents the commands from running continuously without a pause. The following script prints the current time every five seconds.
#!/bin/bash
for attempt in {1..3}; do
date +%H:%M:%S
sleep 5
done
Because sleep 5 also runs after the final iteration, this example includes a final five-second pause before the script exits. Place the delay conditionally when that final pause is not required.
#!/bin/bash
for attempt in {1..3}; do
echo "Attempt: $attempt"
if (( attempt < 3 )); then
sleep 5
fi
done
Using Bash Sleep for Retry Delays
A retry loop can use sleep to wait before trying a failed operation again. The following example attempts to reach a server up to five times and pauses ten seconds between unsuccessful attempts.
#!/bin/bash
url="https://example.com"
for attempt in {1..5}; do
if curl --fail --silent --show-error "$url" > /dev/null; then
echo "Request succeeded"
exit 0
fi
echo "Attempt $attempt failed"
if (( attempt < 5 )); then
sleep 10
fi
done
echo "Request failed after five attempts"
exit 1
Production retry logic often uses increasing delays, maximum retry limits, and command timeouts so that a script does not wait indefinitely.
Running a Command After a Bash Sleep Delay
You can combine sleep with && when the next command should run only if sleep completes successfully.
sleep 30 && echo "Thirty seconds have passed"
To schedule the delayed command in the background, group the commands and append &.
(sleep 30 && echo "Background delay completed") &
The terminal prompt returns immediately while the grouped commands continue as a background job.
Stopping a Bash Sleep Command
When sleep is running in the foreground, press Ctrl+C to send an interrupt signal and stop it. A script can also trap the signal and perform cleanup before exiting.
#!/bin/bash
trap 'echo "Sleep interrupted"; exit 130' INT
sleep 60
echo "Sleep completed"
If the user presses Ctrl+C during the delay, the trap prints a message and exits the script.
Bash Sleep vs Wait
sleep and wait pause a script for different reasons.
| Command | What it waits for | Typical use |
|---|---|---|
sleep | A fixed duration | Delay execution for a specified number of seconds, minutes, hours, or days |
wait | A background process or job to finish | Synchronize commands that were started asynchronously |
The following example starts two background tasks and waits until both have finished.
#!/bin/bash
sleep 3 &
first_pid=$!
sleep 5 &
second_pid=$!
wait "$first_pid"
wait "$second_pid"
echo "Both background tasks completed"
Use sleep when the duration itself matters. Use wait when the script must continue only after a specific background command terminates.
Bash Sleep Command Exit Status
After sleep finishes, Bash stores its exit status in $?. A status of 0 normally means the delay completed successfully. A nonzero status can indicate an invalid argument or an interruption.
sleep 2
echo "Exit status: $?"
Bash Sleep Command FAQs
How do I make a Bash script sleep for one second?
Use sleep 1 or sleep 1s. Seconds are the default unit, so both commands request the same delay.
How do I make Bash sleep for one minute?
Use sleep 1m. You can also write sleep 60, because 60 seconds equals one minute.
How do I make Bash sleep for 10 minutes?
Use sleep 10m. The m suffix tells the command to interpret the number as minutes.
Can Bash sleep for milliseconds?
Many common sleep implementations accept fractional seconds. For example, sleep 0.1 requests a 100-millisecond pause. Verify fractional-value support when the script must run on multiple operating systems.
Does Bash sleep block the entire computer?
No. It pauses only the process running the sleep command. Other applications and processes continue to run. A foreground shell script waits, while a background sleep command allows the interactive shell to accept more commands.
Bash Sleep Tutorial QA Checklist
- Confirm that every
sleepexample uses a valid duration and suffix. - Verify that seconds, minutes, hours, and days are not described as interchangeable without conversion.
- Check fractional-second examples on the target operating systems when portability matters.
- Distinguish a fixed
sleepdelay from waiting for a background process withwait. - Ensure retry-loop examples include a retry limit and do not pause unnecessarily after the final attempt.
- Confirm that long delays can be interrupted or handled safely by the script.
Summary of the Bash Sleep Command
The Bash sleep command delays execution for a specified duration. Use a number without a suffix for seconds, or append s, m, h, or d for seconds, minutes, hours, or days. Fractional values can provide sub-second delays where the installed implementation supports them.
In this Bash Tutorial, we have learnt to delay or pause bash execution by a specified amount of time.
TutorialKart.com