Help setting PS1 prompt to include current time

Hi,

I'm using the ksh shell and I'd like to set my PS1 prompt on an AIX system to include, amongst ther things, the current time.

This was my best effort: export PS1=$(date -u +%R)'${ME}:${PWD}# '
but this only sets the time to the value when PS1 is defined and the time value doesn't actually change after that.

Any one got any ideas how to achieve this? I've tried searching the forum but can't find anything.

Thanks

Gareth

PWD, RANDOM and some other environment variables are defined by the shell to point to a function. I don't know of a function defined as a shell symbol that returns date & time.

In the korn shell try:

PS1='$RANDOM'

Then press return five or six times. There has to be code behind the call to the environment variable becuase it is dynamic.

Try quoting the whole string: export PS1='$(date -u +%R) ${ME}:${PWD}# '

Ygor's solution will work with ksh95 or pdksh. But ksh88 only expands variables...it won't do a general parse. I am not real fond of invoking the date command with each each display of the prompt.

ksh95 has a feature called discipline functions and these can be used to create dynamic variables that work like RANDOM.

Here is a solution that will work with all three versions of ksh. If we set SECONDS, it will continue to advance. So if we set it to the number of seconds since midnight it will serve as a time-of-day clock. To get from seconds-after-midnight to hours:minutes:seconds we need to do some arithmetic. If we reference a imaginary array, we can compute a subscript for it and use side effects from the subscript computation to set other variables. A single date process is used to initialize the clock, but after that the time is computed by the shell itself.

unset _h _m _s
eval $(date "+_h=%H ;_m=%M ;_s=%S")
((SECONDS = 3600*${_h#0}+60*${_m#0}+${_s#0}))
typeset -Z2 _h _m _s
_tsub="(_m=(SECONDS/60%60)) == (_h=(SECONDS/3600%24)) + (_s=(SECONDS%60))"
_timehm='${_x[_tsub]}$_h:${_m}'
_timehms='${_x[_tsub]}$_h:$_m:${_s}'

PS1="$_timehm $ "

1 Like

Hello,
just a 'stupid' question I am using an older ksh (ksh88) and from there I enter into tcsh .... where should I place this code (and how should it be re-written, too), if I want to have it create env variables .... maybe by including it in .cshrc or some other startup files (.login or other)?
Well all that I was thinking of doing was to be able to have these env vars available by the time one of the alias statement inside my .cshrc executes, in such a way I could use the env var contents and place it onto the title bar of my xterm window. Result being I should then get a clock displayed in the title bar.
Do you have any advice or ideas?
Thanks for all!