Operating System - Linux
1822144 Members
3502 Online
109640 Solutions
New Discussion юеВ

Current date & time in posix shell prompt

 
SOLVED
Go to solution
Jaris Detroye
Frequent Advisor

Current date & time in posix shell prompt

I can not seem to find the right PS1 form to show the current time within the command prompt.

Every time I set it, I get the time in the prompt, but it does not change.
I want the time to be dynamic and reflect the current time when the command prompt is displayed.

Any ideas???
9 REPLIES 9
Ivan Krastev
Honored Contributor

Re: Current date & time in posix shell prompt

Hi Jaris,

you must escape date command in PS1 to be run every time, when PS1 is displayed:

export PS1=`hostname`" ""\$(date )"#

cheers,
ivan
Jaris Detroye
Frequent Advisor

Re: Current date & time in posix shell prompt

I just cut & pasted your PS1 string into my unix shell, and since the $ is escaped it simply prints the string $(date) in the prompt is does not substitute the value.
With the PS1 string:
PS1=`hostname`" ""\$(date)"#

This is what I get:
saturn $(date)#

Ivan Krastev
Honored Contributor

Re: Current date & time in posix shell prompt

Oops, its my fault - i tested it in ksh.

ivan
Peter Godron
Honored Contributor
Solution

Re: Current date & time in posix shell prompt

Hi Jaris,
modify your ~/.profile to include:
ENV=~/.shrc
export ENV

then edit/create your ~/.shrc to include:
export DATE="$(date '+%d%b')"
export SECONDS="$(date '+3600*%H+60*%M+%S')"
typeset -Z2 _h; typeset -Z2 _m ; typeset -Z2 _s
_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 "${DATE}${_time} # ")
set +u

Jaris Detroye
Frequent Advisor

Re: Current date & time in posix shell prompt

Wow. (Pause...)

That's some script.
Work's great!
As I try to dissect it to understand exactly what is going on here, I can't seem to understand why the 'export SECONDS=' line produce a dynamic variable that changes every time you reference it.
I tried the date command with a (less complicated) formatted output string, and it produced a static output not changing until the variable is explicitly set again.

Why is this different?
Peter Godron
Honored Contributor

Re: Current date & time in posix shell prompt

Jaris,
a quick search of the archive brought the solution:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1017839
Its the re-evaluation of time that makes the difference to just using `date`.

Script just needed a bit of adjusting.
James R. Ferguson
Acclaimed Contributor

Re: Current date & time in posix shell prompt

Hi Jaris:

David Totsch explained the logic of this solution in this thread:

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=38457&admit=-682735245+1160574500123+28353475

Regards!

...JRF...
Jaris Detroye
Frequent Advisor

Re: Current date & time in posix shell prompt

Thanks for the pointer, I did not come across that thread researching this issue.

I live for these things, my boss thinks it's a waste of time to obsess over these geek things, but this will give me some food for thought to distract me the rest of the day.

Thanks!!!
Jaris Detroye
Frequent Advisor

Re: Current date & time in posix shell prompt

The only modification I made is to include the current path by changing the following line:

export PS1=$(echo "${DATE}${_time}")' @ $PWD #'

Thanks for the lession in obscure shell inner workings.