Skip to main content

How to Measure How Long a Command Takes with time in bash and zsh

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

To see how long a command takes, put time in front of it. The shell runs the command, then reports how long it took. This guide times a one-second pause, with real output from zsh 5.9 and bash 3.2, which report it in different formats.

Syntax​

Syntax
time <command>
PartWhat it means
timeA word of the shell itself: it runs the command and reports how long it took
<command>The command to measure

The bash manual: "If the reserved word time precedes the pipeline, Bash prints timing statistics for the pipeline once it finishes. The statistics currently consist of elapsed (wall-clock) time and user and system time consumed by the command’s execution." The zsh manual: "The pipeline is executed, and timing statistics are reported on the standard error in the form specified by the TIMEFMT parameter."

Prerequisites​

  • zsh or bash. This guide uses zsh 5.9, the default shell on macOS, and bash 3.2.

Step 1: Time a One-Second Pause in zsh​

sleep 1 pauses for one second:

Terminal
time sleep 1
Output
sleep 1 0.00s user 0.00s system 0% cpu 1.014 total

zsh running time sleep 1, which reports sleep 1 0.00s user 0.00s system 0% cpu 1.014 total.

zsh printed one line: the command, the processor time it used (user and system), and total, the time that passed: 1.014 seconds.

Step 2: The Same in bash​

In bash, run the same command:

Terminal
time sleep 1
Output
real 0m1.009s
user 0m0.001s
sys 0m0.001s

bash running time sleep 1, which reports real 0m1.009s, user 0m0.001s and sys 0m0.001s.

bash reports the same things on three lines: real is the time that passed, and user and sys are the processor time. sleep used almost none, because it only waits.

Common Mistakes​

MistakeWhat happensDo this instead
Timing a command that's built into zsh: time echo hizsh printed hi and no timing line. type echo printed echo is a shell builtinTime a separate program: time /bin/echo hi printed its timing line

Troubleshooting​

SymptomLikely causeFix
time printed only the command's output in zshThe command is built into zshTime a separate program, or a script

FAQ​

Why are user and system almost zero?​

Because sleep only waits. zsh reported 0.00s user 0.00s system against a total of 1.014 seconds: sleep spent that second waiting, not computing.

Does the timing go into a file with > file?​

No. time sleep 1 > out.txt still printed its timing line on the screen, and out.txt stayed empty. The zsh manual says the statistics are reported "on the standard error", and > redirects standard output only.

Conclusion​

time sleep 1 reported about one second in both shells: zsh as 1.014 total on one line, bash as real 0m1.009s with user and sys below it. Put time in front of any program to measure it.

Additional Resources​