Clear Night Software

How to make your Unix prompt show the time

This script is designed for ksh, and relies on the fact that Unix increments a variable called SECONDS every second.

If you don't change the value of SECONDS, it contains the number of seconds since you logged in.

Here is the script, which we can call "timeprompt". It inserts the time into your existing ksh prompt.


# Include time in prompt.
export SECONDS="$(date '+3600*%H+60*%M+%S')"
typeset -Z2 _h; typeset -Z2 _m ; typeset -Z2 _s    # 2 digits, zero padded
# hours, minutes and seconds...
_hh="(SECONDS/3600)%24"
_mm="(SECONDS/60)%60"
_ss="(SECONDS)%60"
_time='${_x[(_m=_mm)==(_h=_hh)==(_s=_ss)]}$_h:$_m:$_s'
export PS1=$(echo "${_time}\n${PS1}")

Place timeprompt somewhere in your PATH.
Modify your .profile and insert a line after your PATH and PS1 have been set. Use this command: ". timeprompt"
This version of timeprompt begins by setting SECONDS equal to the time (the number of seconds since midnight).
It then creates three variables that will always contain two-digits and be zero-padded.
The _time variable is set to a formula that uses the evaluation of subscripts in a dummy array _x to format the time.
The last line of timeprompt inserts _time , and a newline character into your prompt.
Replace \n with a space if you want a single line prompt, but this may become too long if you also include values such as ${HOSTNAME}, ${LOGNAME} and ${PWD}

Timeprompt has been tested on AIX and UnixWare. It may require some modification to work with ksh on other versions of Unix.