You need two functions and a timer. First function is executed just after you hit enter on the command line, but before actual command starts. Second function is executed after command finishes, but before prompt is displayed. Timer just counts seconds since you start the shell. In `zsh` these three hooks are called `precmd`, `preexec` and `SECONDS` respectively.
In `bash` timer's name is the same, function `precmd` become a variable `PROMPT_COMMAND`, but unfortunately function `preexec` is missing, so you need to write it yourself (nothing extremely challenging, but not trivial either) or install already written hook from external source, e.g. <
Now we just need to glue all pieces together, minimal code looks like this:
preexec() {
cmd_start="$SECONDS"
}
precmd() {
local cmd_end="$SECONDS"
elapsed=$((cmd_end-cmd_start))
PS1="$elapsed "
}
Put everything in `.bashrc`.