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

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


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


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

trap 'SECONDS=0' DEBUGexport 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' DEBUGPS1='other_prompt_stuff_here ($(seconds2days $SECONDS)) # '

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