An updated version of this post can be found here
I’m fairly certain the following scenario has happened to every
terminal user. You run a command and, while it is running, realize you
should have prefixed it with
momentarily struggle with the thought of killing the command and
rerunning it with
time. You decide not to and the command finishes
without you knowing how long it took. You debate running it again.
For the last year I’ve lived in a world without this problem. Upon completion, a command’s approximate run time is displayed in my prompt. It is awesome.
Most of the code below is from a post on
Stack Overflow. It has
been slightly modified to support having multiple commands in your
$PROMPT_COMMAND variable. Below is a minimal snippet that could be
included in your
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
.bashrc to include the above and you’ll have a prompt
that looks like the image below. It is a minimal prompt but it
includes the time spent on the last command. This is great. No more
wondering how long a command took.
timer_start is a function that sets
timer to be its current value
timer is unset, sets it to the value of
$SECONDS is a special variable that contains the number of seconds
since the shell was started.
timer_start is invoked after every
simple command as a result of
trap 'timer_start' DEBUG.
timer_stop calculates the difference between
and stores it in
timer_show. It also unsets
timer. Next time
timer_start is invoked
timer will be set to the current value of
timer_stop is part of the
is executed prior to the prompt being printed.
It is the interaction between
captures the run time of commands. It is important that
is the last command in the
$PROMPT_COMMAND. If there are other
commands after it then those will be executed and their execution
timer_start to be called. This results in you timing the
length of time between the prior and current prompts being printed.
My prompt is a bit more complicated. It shows the last exit code, last run time, time of day, directory, and git information. The run time of the last command is one of the more useful parts of my prompt. I highly recommend you add it to yours.