The Time Keyword in Bash

The word time is one of the bash shell reserved words. It is not a bash shell builtin.

$ builtin time
bash: builtin: time: not a shell builtin

Bash does support the older Bourne shell keyword times as a builtin. This builtin prints out the user and system times used by the shell and its children.

So where is the time reserved word used in the bash shell? It is primarily intended for printing pipeline timing statistics.

From the current online bash documentation:

The format for a pipeline is

[time [-p]] [!] command1 [| command2 ...]

The output of each command in the pipeline is connected via a pipe to the input of the next command. That is, each command reads the previous command's output.

The reserved word time causes timing statistics to be printed 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 `-p' option changes the output format to that specified by POSIX. The TIMEFORMAT variable may be set to a format string that specifies how the timing information should be displayed.

The popular O’Reilly book UNIX in a Nutshell indicates that time is a builtin for both the Bash and the Korn shells. The book is incorrect; time is not a builtin in the Bash shell.

Comments are closed.