sleep N pauses a script for N seconds. On both GNU coreutils and macOS/BSD, N may be fractional and may carry a s/m/h/d suffix — sleep 0.5, sleep 90s, sleep 2m all work. The thing worth knowing before you write another script: a foreground sleep delays your script’s own signal handlers until it finishes. If your script needs to react to SIGTERM, write sleep 30 & wait $! instead.
Table of Contents
What this shell’s sleep actually accepts
Run on macOS 26.6 with the base-system /bin/sleep, timed with bash’s $EPOCHREALTIME:
sleep 0.5s -> 0.506s
sleep 0.02m -> 1.213s
sleep 0.001h -> 3.603s
sleep 1e-1 -> 0.113s
sleep 0.25 -> 0.264s
Fractions, suffixes, and even scientific notation, because BSD sleep parses the number with strtod(3). GNU coreutils sleep accepts the same fractional and suffixed forms. What neither of them is required to accept is anything at all beyond an integer: the Open Group Base Specifications entry for sleep defines the operand as “a non-negative decimal integer specifying the number of seconds for which to suspend execution”, so sleep 0.5 is a GNU/BSD extension, not a guarantee. In a script that has to run on an unknown /bin/sh — a BusyBox appliance, a minimal container — sleep 1 is the only portable form.
Both flavours also sum multiple operands — the GNU man page says “With multiple arguments, pause for the sum of their values”, and BSD’s says the same. Measured here, sleep 1 2 took 3.047s.
Negative and non-numeric arguments
A negative value is only sometimes an error, and the case where it is not bites in scripts that pass a computed delay:
$ sleep -1
sleep: illegal option -- 1
usage: sleep number[unit] [...]
$ sleep -- -1 # now it is an operand, not an option
$ echo $?
0 # returned in 0.031s — did not sleep, did not complain
So sleep "$delay" where $delay went negative does not reliably fail loudly. It either dies with a confusing option error or silently doesn’t wait. Non-numeric input does fail properly: sleep abc prints sleep: invalid time interval: abc and exits 1.
The signal trap problem, measured
This is the part that turns up as a mysterious 30-second delay during container shutdown. Two scripts, identical except for how they sleep. Both trap SIGTERM. Both were sent SIGTERM one second in:
#!/bin/bash #!/bin/bash
trap 'echo "trap ran at ${SECONDS}s"' TERM trap 'echo "trap ran at ${SECONDS}s"; kill "$pid"' TERM
sleep 5 sleep 5 & pid=$!
wait "$pid"
=== foreground sleep (SIGTERM sent after 1s)
trap ran at 5s
total elapsed 5.278s
=== sleep in background + wait (SIGTERM sent after 1s)
trap ran at 1s
total elapsed 1.047s
Bash runs a trap for a caught signal only after the currently executing foreground command completes — the bash manual’s Signals section states that if bash is waiting for a command to complete and receives a signal for which a trap has been set, it will not execute the trap until the command completes. sleep 5 is that foreground command, so the handler waits out the full five seconds. wait is the exception the same section documents — it returns immediately with an exit status greater than 128 when a trapped signal arrives. That single change is the difference between a graceful shutdown and one that gets SIGKILLed by your init system’s grace period.
Rather than the seconds shown, sleep-in-shell-script-using-bash.sh asserts the ordering: with the same SIGTERM after one second, the foreground version runs its trap late and the sleep & wait version early, and it judges the read -t timeout status, the busy-wait CPU cost and the loop drift below by behaviour in the same way.
An interruptible wait: read -t
read -t SECONDS gives you a delay that something else can cut short. Hold both ends of a FIFO open and the read blocks until either a line arrives or the timeout expires:
f=$(mktemp -u); mkfifo "$f"; trap 'rm -f "$f"' EXIT
exec 3<>"$f" # hold both ends open so the read blocks
if read -t 5 -u 3 -r msg; then echo "woken by $msg"; else echo "timed out (exit $?)"; fi
event at 1s: woken by "wake" after 1.007s
event at 9s: timed out, read exit 142 after 5.001s
The timeout exit status is greater than 128 (142 here — 128 + SIGALRM), which is how you tell “nothing arrived” from “read failed”. read -t also accepts fractional timeouts in bash. Anything writing a line to that FIFO — a health check, a signal handler, another job — ends the wait early.
Busy-waiting costs about 800x more CPU
Polling in a loop instead of sleeping is a common workaround when a delay “feels” imprecise. Two seconds of waiting, three ways, timed with bash’s time:
$ time sleep 2
real 0m2.011s user 0m0.001s sys 0m0.001s
$ time { end=$((SECONDS+2)); while [ $SECONDS -lt $end ]; do :; done; }
real 0m1.597s user 0m1.578s sys 0m0.014s
$ time { s=$(date +%s); while [ $(( $(date +%s) - s )) -lt 2 ]; do :; done; }
real 0m1.992s user 0m0.583s sys 0m0.974s
sleep burned 2 milliseconds of CPU. The pure-bash spin burned 1.59 seconds — the process is runnable the entire time, so on a loaded box it takes a core away from work that needs it. The date version is worse in a different way: 0.97s of that is system time from forking a process per iteration. A sleeping process is off the run queue and costs nothing.
Loop drift
sleep guarantees a minimum, not an exact interval, and each call is a fresh process:
10 x sleep 0.1 in a loop : 1.092s (ideal 1.000s)
one sleep 1 : 1.006s
Roughly 9 ms of overhead per iteration here, and it accumulates. For a loop that must run on a wall-clock schedule, compute the remaining time each pass from a start timestamp rather than sleeping a fixed amount, or hand the schedule to cron / a systemd timer.
Not tested here
This machine is macOS, so GNU coreutils sleep behaviour is quoted from its manual (“Pause for NUMBER seconds, where NUMBER is an integer or floating-point. SUFFIX may be ‘s’,’m’,’h’, or ‘d’ … With multiple arguments, pause for the sum of their values”), not measured here. The POSIX wording is from the Open Group Base Specifications Issue 8 entry for sleep. Everything in a measured code block came from the commands shown, on this machine, on 2026-08-10.