Bash extension for printing timing information for each command line executed.
After the execution of each command line, the script prints out the total
execution time (up to millisecond precision), followed by the current time. The
execution time is formatted to be human readable; e.g.,
2h 7m 42s301.
This script should run pretty much out of the box on modern Linux and Mac OS X systems. Please report any incompatibilities on on GitHub.
Note that this script will also conflict with any other script that uses the
DEBUG trap and
To set up this extension, you can
bash_command_timer.shsomewhere, and add the following to your
~/.bashrc(replace with actual path where you saved the script):
Alternatively, you can simply copy and paste the contents of
That's it :)
You can use the following options to tweak the behavior of the script. You can
either make the changes in-place (at the top of the script) or put them after
sourcing the script in your
.bashrc. You can also modify them on-the-fly if
you want the changes to only affect your current Bash session.
BCT_ENABLE=1: Setting this to 0 disables the printing of timings.
BCT_COLOR='34': The color of the output. This should be a color string usable in a VT100 escape sequence (see Wikipedia), without the escape sequence prefix and suffix. For example, bold red would be
BCT_TIME_FORMAT='%b %d %I:%M%p': The display format of the current time. This is a strftime format string (see http://strftime.org/). If empty, the current time will not be printed.
BCT_MILLIS=1: Whether to print timings to millisecond precision. If set to zero, will print timings up to seconds.
For an explanation of how the script works, you're welcome to check out my blog post: DEBUG trap and PROMPT_COMMAND in Bash.