2

Is there a way to show time elapsed since I started last command? I found this: PS1 prompt to show elapsed time but that will show time elapsed since last command finished until the new command finished.

My idea is to somehow force the prompt to add timebefore every command I type in, and the format it in some nice way.

Something like this:

$ ls
. ..
Last command took 0.001s
$ 
  • Not exactly an answer, but zsh has some premade themes that do this easily (I use powerlevel9k). It's highly customisable, but can include duration of the last command. I highly recommend zsh over bash anyway for other reasons. – Sparhawk Dec 4 '18 at 1:11
  • Is unix.stackexchange.com/q/252229/117549 close to what you're asking? – Jeff Schaller Dec 4 '18 at 2:01
  • @JeffSchaller It is the exact same question as I linked. – klutt Dec 4 '18 at 2:08
  • You want a time needed for the last command execution, do I understand it correctly? – jimmij Dec 4 '18 at 5:33
  • @jimmij That is correct – klutt Dec 4 '18 at 5:38
3

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. https://github.com/rcaloras/bash-preexec.

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.

  • I don't get it. Do you mean that I should manually type in precmd in front of every command? – klutt Dec 4 '18 at 6:30
  • @Broman Of course not, you define precmd and preexec once in .bashrc (or any init file) and these functions are treated specially by the shell. Before everything however install the script I linked. – jimmij Dec 4 '18 at 6:36
  • I copied everything to .bashrc, commented out my old prompt, ran source .bashrc and restarted the terminal but my prompt does not show the execution time of last command. – klutt Dec 4 '18 at 13:36
  • What exactly did you put into .bashrc? In interactive bash run curl https://raw.githubusercontent.com/rcaloras/bash-preexec/master/bash-preexec.sh -o ~/.bash-preexec.sh, then source ~/.bash-preexec.sh, and then copy-paste two functions from my answer. Does that work? – jimmij Dec 4 '18 at 15:39
  • Yes that worked. I did not understand that I needed to get the bash-preexec.sh. Thought it would be enough to copypaste preexec and precmd to .bashrc. However, I would like to include this number in my existing prompt. Do you know how to do that? – klutt Dec 4 '18 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.