Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Processes and Signals

Bash time command

Reference for the Bash time keyword and GNU time: measure real, user and system time of a command or pipeline, customize TIMEFORMAT, and read peak memory with /usr/bin/time.

time runs a command and reports how long it took. There are two versions. The one you normally use is a Bash keyword: it times a whole pipeline and prints three lines, real, user and sys, to standard error. The other is the program /usr/bin/time (GNU time), which has options and can also report memory use and exit status.

The three numbers answer different questions. real is the wall-clock time you waited. user is CPU time spent running the program's own code, and sys is CPU time spent in the kernel on its behalf. On several cores user + sys can exceed real; while waiting on a disk, network or a sleep, it is much smaller.

Terminal
time PIPELINE
/usr/bin/time [-v] [-f FORMAT] [-o FILE] COMMAND [ARG]...

Try it

Bash Waiting versus computing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Format, status and pipelines

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

GNU time is not installed on the live runner, so this one is read-only:

Terminal
/usr/bin/time -f '%e s, %M KB peak, exit %x' sort big.txt > /dev/null
# 0.42 s, 15840 KB peak, exit 0

Syntax

Terminal
time [-p] PIPELINE
TIMEFORMAT=FORMAT
Piece Description
time -p Portable POSIX format: real 0.50, user 0.00, sys 0.00, one per line, seconds.
TIMEFORMAT Variable that sets the Bash output format.
%R, %U, %S Real, user and system seconds.
%P CPU percentage, (user+sys)/real.
%lR, %lU, %lS Long form: 1m2.500s.
%[N]R Number of decimals from 0 to 3.
%% A literal percent sign.

GNU /usr/bin/time has its own placeholders: -f with %e (elapsed seconds), %U, %S, %M (peak memory in KB), %x (exit status), and -v for a full report.

Returns: the exit status of the timed command or pipeline.

Usage notes

  • Output goes to stderr. To capture it, redirect the group: { time cmd; } 2> timing.txt. Putting 2> after time cmd only redirects the command's own errors.
  • Keyword vs program. time alone is the keyword; command time or \time or /usr/bin/time runs the program. Only the program offers -v and memory data.
  • Pipelines. The Bash keyword measures the whole pipeline. The external program times one simple command, not shell syntax like pipes.
  • Not a benchmark by itself. One run is noisy: file caches, CPU frequency and other processes matter. Repeat several times and compare the minimum or median.
  • Resolution. Times are accurate to a few milliseconds. For shorter things, loop the command many times.
  • Subshell trap. time applies to the command that follows, so time cd /tmp times a builtin; time (a; b) times the group.
  • dash (/bin/sh) has no time keyword, so portable scripts need /usr/bin/time.