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

后端 未结 14 2661
無奈伤痛
無奈伤痛 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条回答
  • 2020-12-04 06:13

    Another approach for bash 4.x and above would be to use coproc with PS0 and PS1 like below:

    cmd_timer()
    {
        echo $(( SECONDS - $(head -n1 <&"${CMD_TIMER[0]}") ))
    }
    
    coproc CMD_TIMER ( while read; do echo $SECONDS; done )
    echo '' >&"${CMD_TIMER[1]}" # For value to be ready on first PS1 expansion
    export PS0="\$(echo '' >&${CMD_TIMER[1]})"
    export PS1="[ \$(cmd_timer) ] \$"
    

    This is a .bashrc ready snippet. It is especially useful for everyone that uses undistract-me which overwrites trap DEBUG for its own purposes.

    0 讨论(0)
  • 2020-12-04 06:16

    If you hadn't set up any of the other answers before you kicked off your long-running job and you just want to know how long the job took, you can do the simple

    $ HISTTIMEFORMAT="%s " history 2
    

    and it will reply with something like

      654  1278611022 gvn up
      655  1278611714 HISTTIMEFORMAT="%s " history 2
    

    and you can then just visually subtract the two timestamps (anybody know how to capture the output of the shell builtin history command?)

    0 讨论(0)
提交回复
热议问题