Show time elapsed since I started last command in prompt

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite
1












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
$









share|improve this question























  • 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 at 1:11











  • Is unix.stackexchange.com/q/252229/117549 close to what you're asking?
    – Jeff Schaller
    Dec 4 at 2:01










  • @JeffSchaller It is the exact same question as I linked.
    – Broman
    Dec 4 at 2:08










  • You want a time needed for the last command execution, do I understand it correctly?
    – jimmij
    Dec 4 at 5:33










  • @jimmij That is correct
    – Broman
    Dec 4 at 5:38














up vote
2
down vote

favorite
1












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
$









share|improve this question























  • 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 at 1:11











  • Is unix.stackexchange.com/q/252229/117549 close to what you're asking?
    – Jeff Schaller
    Dec 4 at 2:01










  • @JeffSchaller It is the exact same question as I linked.
    – Broman
    Dec 4 at 2:08










  • You want a time needed for the last command execution, do I understand it correctly?
    – jimmij
    Dec 4 at 5:33










  • @jimmij That is correct
    – Broman
    Dec 4 at 5:38












up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





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
$









share|improve this question















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
$






bash prompt






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 4 at 2:08

























asked Dec 4 at 0:58









Broman

154111




154111











  • 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 at 1:11











  • Is unix.stackexchange.com/q/252229/117549 close to what you're asking?
    – Jeff Schaller
    Dec 4 at 2:01










  • @JeffSchaller It is the exact same question as I linked.
    – Broman
    Dec 4 at 2:08










  • You want a time needed for the last command execution, do I understand it correctly?
    – jimmij
    Dec 4 at 5:33










  • @jimmij That is correct
    – Broman
    Dec 4 at 5:38
















  • 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 at 1:11











  • Is unix.stackexchange.com/q/252229/117549 close to what you're asking?
    – Jeff Schaller
    Dec 4 at 2:01










  • @JeffSchaller It is the exact same question as I linked.
    – Broman
    Dec 4 at 2:08










  • You want a time needed for the last command execution, do I understand it correctly?
    – jimmij
    Dec 4 at 5:33










  • @jimmij That is correct
    – Broman
    Dec 4 at 5:38















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 at 1:11





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 at 1:11













Is unix.stackexchange.com/q/252229/117549 close to what you're asking?
– Jeff Schaller
Dec 4 at 2:01




Is unix.stackexchange.com/q/252229/117549 close to what you're asking?
– Jeff Schaller
Dec 4 at 2:01












@JeffSchaller It is the exact same question as I linked.
– Broman
Dec 4 at 2:08




@JeffSchaller It is the exact same question as I linked.
– Broman
Dec 4 at 2:08












You want a time needed for the last command execution, do I understand it correctly?
– jimmij
Dec 4 at 5:33




You want a time needed for the last command execution, do I understand it correctly?
– jimmij
Dec 4 at 5:33












@jimmij That is correct
– Broman
Dec 4 at 5:38




@jimmij That is correct
– Broman
Dec 4 at 5:38










1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










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.






share|improve this answer






















  • I don't get it. Do you mean that I should manually type in precmd in front of every command?
    – Broman
    Dec 4 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 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.
    – Broman
    Dec 4 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 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?
    – Broman
    Dec 4 at 15:47










Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f485798%2fshow-time-elapsed-since-i-started-last-command-in-prompt%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










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.






share|improve this answer






















  • I don't get it. Do you mean that I should manually type in precmd in front of every command?
    – Broman
    Dec 4 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 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.
    – Broman
    Dec 4 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 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?
    – Broman
    Dec 4 at 15:47














up vote
2
down vote



accepted










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.






share|improve this answer






















  • I don't get it. Do you mean that I should manually type in precmd in front of every command?
    – Broman
    Dec 4 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 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.
    – Broman
    Dec 4 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 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?
    – Broman
    Dec 4 at 15:47












up vote
2
down vote



accepted







up vote
2
down vote



accepted






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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 4 at 6:31

























answered Dec 4 at 6:24









jimmij

30.5k869103




30.5k869103











  • I don't get it. Do you mean that I should manually type in precmd in front of every command?
    – Broman
    Dec 4 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 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.
    – Broman
    Dec 4 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 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?
    – Broman
    Dec 4 at 15:47
















  • I don't get it. Do you mean that I should manually type in precmd in front of every command?
    – Broman
    Dec 4 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 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.
    – Broman
    Dec 4 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 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?
    – Broman
    Dec 4 at 15:47















I don't get it. Do you mean that I should manually type in precmd in front of every command?
– Broman
Dec 4 at 6:30




I don't get it. Do you mean that I should manually type in precmd in front of every command?
– Broman
Dec 4 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 at 6:36




@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 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.
– Broman
Dec 4 at 13: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.
– Broman
Dec 4 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 at 15:39




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 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?
– Broman
Dec 4 at 15:47




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?
– Broman
Dec 4 at 15:47

















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f485798%2fshow-time-elapsed-since-i-started-last-command-in-prompt%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?