beemtee
beemtee

Reputation: 941

Including process execution time into shell prompt

Is it possible to include the execution time of the just finished process - the output of the 'time' command - into the command prompt? We use zsh on linux and bash (msys) on windows machines so it would be nice to know how to do it in both.

Upvotes: 4

Views: 1665

Answers (1)

ZyX
ZyX

Reputation: 53674

For zsh you can try $REPORTTIME variable (search man zshall for it). It won't put time into the prompt, but it will echo time after each executed command (does not work in some cases). You can use preexec and precmd hooks and and $SECONDS variable to get execution time (but it is not too precise):

function preexec() {
    typeset -gi CALCTIME=1
    typeset -gi CMDSTARTTIME=SECONDS
}
function precmd() {
    if (( CALCTIME )) ; then
        typeset -gi ETIME=SECONDS-CMDSTARTTIME
    fi
    typeset -gi CALCTIME=0
}
PS1='${ETIME} %'

(Note single quotes used in PS1 definition.)

Third approach will either force you to loose ability to change environment variables or will work only for single commands. It will also force you to write something hacky to view command output. This approach is “redefine accept-line zle widget to add time to the command executed”. I am not writing code here because it is has problems described above.

By the way, $SECONDS variable is also available in bash, though I do not know how to implement hooks there.

Upvotes: 5

Related Questions