Show time elapsed since I started last command in prompt
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
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 time
before every command I type in, and the format it in some nice way.
Something like this:
$ ls
. ..
Last command took 0.001s
$
bash prompt
add a comment |
up vote
2
down vote
favorite
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 time
before every command I type in, and the format it in some nice way.
Something like this:
$ ls
. ..
Last command took 0.001s
$
bash prompt
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
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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 time
before every command I type in, and the format it in some nice way.
Something like this:
$ ls
. ..
Last command took 0.001s
$
bash prompt
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 time
before every command I type in, and the format it in some nice way.
Something like this:
$ ls
. ..
Last command took 0.001s
$
bash prompt
bash prompt
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
add a comment |
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
add a comment |
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
.
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, ransource .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 runcurl https://raw.githubusercontent.com/rcaloras/bash-preexec/master/bash-preexec.sh -o ~/.bash-preexec.sh
, thensource ~/.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
|
show 3 more comments
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
.
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, ransource .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 runcurl https://raw.githubusercontent.com/rcaloras/bash-preexec/master/bash-preexec.sh -o ~/.bash-preexec.sh
, thensource ~/.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
|
show 3 more comments
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
.
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, ransource .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 runcurl https://raw.githubusercontent.com/rcaloras/bash-preexec/master/bash-preexec.sh -o ~/.bash-preexec.sh
, thensource ~/.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
|
show 3 more comments
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
.
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
.
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, ransource .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 runcurl https://raw.githubusercontent.com/rcaloras/bash-preexec/master/bash-preexec.sh -o ~/.bash-preexec.sh
, thensource ~/.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
|
show 3 more comments
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, ransource .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 runcurl https://raw.githubusercontent.com/rcaloras/bash-preexec/master/bash-preexec.sh -o ~/.bash-preexec.sh
, thensource ~/.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
|
show 3 more comments
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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