Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Does anyone know a mean of putting a 'time' command before every command in a bash session?

share|improve this question
so... you want to automatically prepend 'time' to every commandline? – quack quixote Mar 7 '10 at 19:27
yes, excaptly ... for performance mesuarament obviously – jthoenes Mar 7 '10 at 19:39
related… – lesmana Aug 25 '10 at 2:40
up vote 33 down vote accepted

Sorry for the wrong answer before, I missunderstood your question.

To have the time added before every command that you execute on the shell you can do something like this

bind 'RETURN: "\e[1~time \e[4~\n"'

This will rebind the return key. Now every time you press return instead of writing a newline \n it will go to the beginning of the line, enter the text 'time' and a space, go to the end of the line and enter the newline \n thereby producing the desired effect.

If you don't want to sacrifice your Enter Key you could make a 'second' benchmark-enter Key like F12 by binding the command like this

bind '"\e[24~": "\e[1~time \e[4~\n"'

Now instead of replacing the return key you bound F12.

The background of all this is that bash uses GNU readline to read commands. So readline would be a good starting point for further command manipulation, etc.

share|improve this answer
+1 nice solution! i like the idea of having a benchmarking enter key separate from the regular enter key. – quack quixote Mar 11 '10 at 5:13
+1 Cool Solution! – sixtyfootersdude Mar 26 '10 at 21:10
that's a clever idea. How can you generalize it to execute a user-defined function before every command? I don't know how to combine bash code and a readline function in the same binding. – Gilles Aug 14 '10 at 11:29

I realize that this is outside of the scope of this question but...

In zsh (which, is to my knowledge a super set of bash) if you set the following variable in your .zshrc file:


Every command that takes longer than 5 seconds (I'm pretty sure) will display the output of time. All commands that complete more quickly don't. And in those cases one doesn't really care, so it's nice to not clutter things up. There are also a lot of other cool features in zsh that you might enjoy while you're at it.

share|improve this answer
Nice tip. While zsh surely has a superset of the functionality of bash, its syntax is not a superset of the syntax of bash, so it is not a “drop in” replacement. – Chris Johnsen Mar 8 '10 at 6:11

PS1 seems like the standard way to do something like this.

share|improve this answer
It modifies the display, not the actual executed command. – grawity Mar 8 '10 at 15:21
@grawity Right - but it would get you exactly what you want ... the time prepended to every line. – Jason Sundram Mar 10 '10 at 1:29
Which is completely different from what time does (it measures how long did it take for a command to execute). – grawity Mar 10 '10 at 12:29
@grawity, I guess you're right -- you'd have to do the subtraction yourself. Looks like @Jrobert had the same idea. – Jason Sundram Mar 10 '10 at 18:20

Include \t in your prompt string. It will include the time in your prompt at the point where you include it. See:

share|improve this answer
That's the current time at the time the prompt is generated, not the time it took to execute the previous command. – Dennis Williamson Mar 8 '10 at 6:28
True; and the difference between that and the next prompt's current time is the run-time (minus typing time, of course - pasting is your friend here). No key reassignments necessary. – JRobert Mar 8 '10 at 13:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .