Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 superuser.com/questions/167283/… –  lesmana Aug 25 '10 at 2:40

4 Answers 4

up vote 28 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

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

share|improve this answer
1  
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
2  
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: http://www.ibm.com/developerworks/linux/library/l-tip-prompt/

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

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:

export REPORTTIME=5

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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