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

老子叫甜甜 提交于 2019-12-02 23:34:00
Dennis Williamson

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

willdye

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!