My article on putting the runtime of your last command into your bash prompt is one of my most surfaced-by-google articles. Why is this a great to your prompt? To quote my previous article:
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
time. You 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.
I’ve been living without the above problem since sometime in 2014 and not having that problem is still awesome.
I have made some changes since 2014.
One change was switching to using Bash-Preexec instead of directly using
$PROMPT_COMMAND for calling functions to start and stop tracking runtime.
Bash-Preexec lets you trigger a function (or multiple) right after a command has been read and right before each prompt.
The usage is pretty straight forward.
In the most basic case, you source
bash-preexec.sh and then provide functions named
preexec, which is invoked right before a command is executed, and/or
precmd, which is invoked just before each prompt.
bash-preexec.sh can be downloaded from its repo.
The changes required to move to Bash-Preexec pretty pretty minimal.
Here is a screenshot of everything in action (with a reduced prompt, my normal one includes git and other info).
Below is a simplified snippet from my
.bashrc that provides runtimes using both of these additions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
No more wondering about the runtime of commands is great.
format-duration made reading the time easier while Bash-Preexec made reading the implementation easier.
I highly recommend setting up something similar for your shell.