Including process runtime in a shell prompt

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

+4
source share
1 answer

For zsh, you can try the $REPORTTIME variable (search man zshall for it). It will not put the time in the tooltip, but will be an echo after each command executed (in some cases this does not work). You can use preexec and precmd hooks and $SECONDS to get runtime (but this is not too accurate):

 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 the single quotes used in the definition of PS1.)

The third approach will cause you to lose the ability to change environment variables or will work only for single commands. It will also force you to write something hacked to view the output of the command. This approach is to "override the zle accept-line zle widget to add time to the executed command." I am not writing code here because it has the problems described above.

By the way, the $SECONDS variable is also available in bash, although I don’t know how to implement interceptors there.

+5
source

All Articles