How can the last command's wall time be put in the Bash prompt?

后端 未结 14 2846
無奈伤痛
無奈伤痛 2020-12-04 05:16

Is there a way to embed the last command\'s elapsed wall time in a Bash prompt? I\'m hoping for something that would look like this:

[last: 0s][/my/dir]$ sl         


        
14条回答
  •  旧时难觅i
    2020-12-04 05:54

    Will putting a \t in PS1 work for you?

    It does not give the elapsed time but it should be easy enough to subtract the times when necessary.

    $ export PS1='[\t] [\w]\$ '
    [14:22:30] [/bin]$ sleep 10
    [14:22:42] [/bin]$
    

    Following the OP's comment that he is already using \t. If you can use tcsh instead of bash, you can set the time variable.

    /bin 1 > set time = 0
    /bin 2 > sleep 10
    0.015u 0.046s 0:10.09 0.4%      0+0k 0+0io 2570pf+0w
    /bin 3 >
    

    You can change the format of the printing to be less ugly (se the tcsh man page).

    /bin 4 > set time = ( 0 "last: %E" )
    /bin 5 > sleep 10
    last: 0:10.09
    /bin 6 >
    

    I do not know of a similar facility in bash

提交回复
热议问题