People often search for bash measure time of command, bash execution time, bash time a command, bash elapsed time, or bash time script. This page covers the usual cases: timing from the shell (external) and measuring wall clock vs CPU time, plus timing from inside a script so you always print duration—even on failure.
Tested on GNU Bash 5.2 on Linux (2026-06-13):
time sleep 1,TIMEFORMAT,SECONDS,EPOCHREALTIME+bc,trap EXIT.
Bash time a command (external)
Run a command or script under Bash’s time reserved word. It reports real (elapsed wall clock), user, and sys (CPU in user and kernel mode).
time sleep 1Example output:
real 0m1.017s
user 0m0.001s
sys 0m0.002s- real — wall clock from start to finish (what people mean by bash elapsed time or bash execution time for “how long did it take?”).
- user / sys — CPU time; useful when the process waits on I/O or other jobs, where real can be much larger than user + sys.
To bash measure execution time in a compact numeric form, set TIMEFORMAT (Bash builtin time only; does not affect /usr/bin/time):
TIMEFORMAT='real %3R sec (user %3U sys %3S)'
time sleep 1Example:
real 1.017 sec (user 0.001 sys 0.002)TIMEFORMAT supports %R (real seconds), %U (user), %S (sys), and more—see help TIMEFORMAT in Bash.
GNU time from time package is a separate binary; use command time or /usr/bin/time if you need its options (for example -f format on GNU).
Bash time script (one file from outside)
Same as timing any command:
time bash ./myscript.sh
# or
time ./myscript.sh # if executable and has correct shebangThat answers bash measure time of command / bash time script without changing the script.
Bash measure time inside the script (elapsed)
Integer seconds: SECONDS
Bash increments the SECONDS variable by one each second after you assign to it. Good for rough bash execution time:
#!/usr/bin/env bash
SECONDS=0
sleep 2
echo "SECONDS=$SECONDS"Example:
SECONDS=2Subsecond elapsed: EPOCHREALTIME (Bash 5+)
For fractional bash elapsed time / bash time elapsed without date + bc on every line:
#!/usr/bin/env bash
start=$EPOCHREALTIME
sleep 0.2
echo "scale=3; $EPOCHREALTIME - $start" | bc -lExample (approximate):
.226384On older Bash, use date +%s.%N at start and end and subtract with bc or awk—same idea as the original article, just keep bc available or use integer date +%s for whole seconds only.
Always print duration: trap on EXIT
To bash measure time even when the script errors out, compute duration in a function and run it on exit:
#!/usr/bin/env bash
start=$EPOCHREALTIME
report_duration() {
local sec
sec=$(echo "scale=2; $EPOCHREALTIME - $start" | bc -l)
echo "Elapsed: ${sec}s" >&2
}
trap report_duration EXIT
# ... rest of script ...Use SECONDS instead of bc if you only need whole seconds and want zero dependencies.
Summary
| Goal | Approach |
|---|---|
| Bash time a command / measure from the shell | time cmd, optional TIMEFORMAT |
| Bash time script externally | time ./script.sh or time bash script.sh |
| Wall clock vs CPU | real vs user + sys in time output |
| Inside script, whole seconds | SECONDS=0 … echo $SECONDS |
| Inside script, fractional | EPOCHREALTIME + bc (Bash 5+) |
| Always show duration | trap '...' EXIT |
If you need per-line timing for debugging, enable set -x and customize PS4 to include timestamps—that is a different tool (trace overhead), not a substitute for time on the whole command.
For related topics, see get script name and path in a shell script. When the goal is a wall-clock stop time for a loop (not timing a single command), use run a while loop until a specific time in Bash instead.

