Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically timing every executed command and show in Bash prompt? [duplicate]

Tags:

bash

time

prompt

I often happen to forget to explicitly prefix executions with the "time" command, ideally I would see in the next shell prompt how much real time the last command took (on every command).

I already scoured through the bash documentation, but couldn't find anything related.

like image 339
mark Avatar asked Jul 24 '09 08:07

mark


People also ask

How do you time a bash script?

Using Bash Shell's TIMEFORMAT The TIMEFORMAT is a string format that will be printed after the execution of the block code inside the time{} wrapper finishes. The %R specifies to print the elapsed time in seconds with milliseconds precision. Let's test our script: $ ./elapsed_time.sh It took 12.008 seconds.


1 Answers

You could do this:

$ bind '"\C-j": "\C-atime \C-m"'

Or put this in your ~/.inputrc:

"\C-j": "\C-atime \C-m"

Then when you want to do time sleep 1 you'd type sleep 1 and press Ctrl+J instead of Enter.

I would not recommend swapping the j and m in the bind command (or in the .inputrc file). Every time you'd press Enter you'd get time added which could be pretty annoying and would cause errors when typing a multi-line command.

You could add this to your ~/.bashrc to make the output of time more compact:

export TIMEFORMAT='r: %R, u: %U, s: %S'

(similar to my answer here.)

like image 186
Dennis Williamson Avatar answered Sep 27 '22 23:09

Dennis Williamson