Linux - Control the time format returned by time

By xngo on June 30, 2019

In Linux, time command is used to time the execution time of script or simple command. By default, it returns real, user and sys. However, you can control the time format returned by time by changing the TIMEFORMAT variable.

Here are some examples.

# Bash built-in time
TIMEFORMAT=%R; time sleep 2s
#2.003
 
TIMEFORMAT=%lR; time sleep 2s
#0m2.005s
 
TIMEFORMAT='%R %U %S %P'; time sleep 2s
# 2.002 0.002 0.001 0.11
 
TIMEFORMAT="%lR %lU %lS"; time sleep 2s
# 0m2.002s 0m0.002s 0m0.000s
  • %E: Elapsed real time (in [hours:]minutes:seconds).
  • %R: Return real time.
  • %U: Total number of CPU-seconds that the process spent in user mode.
  • %S: Total number of CPU-seconds that the process spent in kernel mode.
  • %P: Percentage of the CPU that this job got, computed as (%U + %S) / %E.

Reference

  • https://linux.die.net/man/1/time

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.