A Bash timed pipeline utilizes theDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
time reserved word to measure and report the execution time and system resource utilization of a command pipeline. Because time is a shell keyword rather than an external executable (like /usr/bin/time), it evaluates the entire pipeline’s execution context, waiting for all constituent commands to terminate before calculating the aggregate resource usage.
Syntax
-p: Forces the output to conform to the POSIX standard format.
Execution Mechanics
When thetime keyword precedes a pipeline, Bash alters its standard execution flow:
- Process Spawning: Bash spawns subshells for the commands in the pipeline.
- Blocking: The shell blocks and waits for the termination of all processes within the pipeline, regardless of their individual exit statuses.
- Aggregation: Bash aggregates the timing statistics from the kernel for all processes in the pipeline.
- Output Routing: The timing statistics are written directly to standard error (
stderr), bypassing standard output (stdout). This ensures the timing data does not interfere with the pipeline’s standard data stream. - Exit Status: The exit status of the timed pipeline is the exit status of the pipeline itself (typically the exit status of the last command, unless
set -o pipefailis active).
Redirection Mechanics
Becausetime is a shell reserved word, standard file descriptor redirection applied at the end of a pipeline affects only the commands within the pipeline, not the time keyword itself. For example, executing time command1 | command2 2> time.txt redirects the standard error of command2, but the timing statistics generated by time will bypass this redirection and print directly to the terminal.
To successfully redirect the standard error output of the time keyword, the entire timed pipeline must be enclosed within a command group or a subshell. The redirection is then applied to the enclosing structure:
Reported Metrics
By default, thetime keyword reports three specific metrics:
- real: Wall-clock time elapsed from the pipeline’s invocation to its complete termination.
- user: Total amount of CPU time spent executing in user space across all processes in the pipeline.
- sys: Total amount of CPU time spent executing in kernel space (system calls) across all processes in the pipeline.
user and sys time can exceed the real time if the pipeline’s constituent commands execute concurrently on different CPU cores.
Output Formatting
The output format of thetime keyword is controlled by the TIMEFORMAT shell variable. It does not need to be exported to the environment for the time reserved word to recognize and use it. If TIMEFORMAT is unset, Bash uses a default human-readable format.
The variable accepts format specifiers prefixed with %:
TIMEFORMAT specifiers:
%R: Elapsed real time in seconds.%U: Number of CPU seconds spent in user mode.%S: Number of CPU seconds spent in system mode.%P: CPU percentage, computed as(%U + %S) / %R.
Keyword vs. External Command
The distinction between the Bash keyword and the external binary is critical when evaluating pipelines.Master Bash with Deep Grasping Methodology!Learn More





