tags:

views:

140

answers:

2

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.

+2  A: 

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.)

Dennis Williamson
+1 interesting trick.
neuro
Great, thank you! (sorry it took so long to reply)
mark
A: 

Another stackoverflow thread covers the essentially the same question. My answer in that thread can be summarized as:

trap 'SECONDS=0' DEBUG
export PS1='your_normal_prompt_here ($SECONDS) # '

...to display the number of seconds as an integer, or:

seconds2days() { # convert integer seconds to Ddays,HH:MM:SS
    printf "%ddays,%02d:%02d:%02d" $(((($1/60)/60)/24)) \
    $(((($1/60)/60)%24)) $((($1/60)%60)) $(($1%60)) |
    sed 's/^1days/1day/;s/^0days,\(00:\)*//;s/^0//' ; }
trap 'SECONDS=0' DEBUG
PS1='other_prompt_stuff_here ($(seconds2days $SECONDS)) # '

..for "Ddays,HH:MM:SS" with leading empty values removed.

willdye