Bash measure time of command and script (execution time, elapsed time)

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).

bash
time sleep 1

Example output:

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):

bash
TIMEFORMAT='real %3R sec (user %3U sys %3S)'
time sleep 1

Example:

text
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:

bash
time bash ./myscript.sh
# or
time ./myscript.sh   # if executable and has correct shebang

That 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:

bash
#!/usr/bin/env bash
SECONDS=0
sleep 2
echo "SECONDS=$SECONDS"

Example:

text
SECONDS=2

Subsecond elapsed: EPOCHREALTIME (Bash 5+)

For fractional bash elapsed time / bash time elapsed without date + bc on every line:

bash
#!/usr/bin/env bash
start=$EPOCHREALTIME
sleep 0.2
echo "scale=3; $EPOCHREALTIME - $start" | bc -l

Example (approximate):

text
.226384

On older Bash, use date +%s.%N at start and end and subtract with bc or awk command—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:

bash
#!/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.

Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with more than 15 years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, networking, and security, delivering robust and efficient solutions for diverse projects.

  • Go (programming language)
  • Python (programming language)
  • DevOps
  • Computer Security
  • Cloud Computing
  • Kubernetes
  • Linux
  • Ansible (software)