How to display current time in the prompt

Hello All,

I would like to display the current time in prompt.

I tried using following command:

export PS1="$(date +%k:%M:%S) $ "

but it gave me a fixed time in prompt whereas my objective is to get the current time everytime.

$ export PS1="$(date +%k:%M:%S) $ "
17:42:42 $
17:42:42 $ date
Tue Oct  2 17:42:54 PDT 2012
17:42:42 $

Please help.

Thank you - Manish

export INTERVAL=1 
export SCRIPT="something.py" 
 
while [ true ] 
    do 
    clear 
    python ${SCRIPT} 
    sleep ${INTERVAL} 
    done 

not sure if it helps.. googled..

If your shell is bash you can use:

PS1='\D{%k:%M:%S} $ '

For ksh95/pdksh try:

PS1='$(date +%k:%M:%S) $ '

Otherwise try this: Help setting PS1 prompt to include current time

Hello,

Thank you for replying but both the options are not working.

Please suggest other options to display the current time in prompt.

Hey,

Thats works for me:

export PS1=$(date +%k:%M:%S)$

What shell and OS do you use?

I use ksh.

bash

trap 'PS1="$(date ) > "' 17

ksh

trap 'PS1="$(date ) > "' 47

I don't see a problem setting both traps in either shell. (date +...) whatever format in each system can be used.

Hello rdrtx1,

Thanks for your reply. I am not much familiar with unix options, so I tried to google trap command and found that it catches a signal sent to the process.

I tried executing it on prompt, nothing happens (no prompt change).

Can you please guide me if there is another way of running this command.

The trap is from a process signal. Hitting the return key with an empty line does not generate a process therefore no signal is generated. So every command executed at the prompt should return a signal and the trap command will execute.

Thank you for explaining the trap.

But I am not achieving my goal to get current time in prompt using:

trap 'PS1="$(date ) > "' 47

Please suggest.

try:

trap 'PS1="$(date ) > "' SIGCHLD

Executed this as well but no luck.

$ trap 'PS1="$(date ) > "' SIGCHLD
$

Defined signals on bash (gnu/linux) are:

EXIT
SIGHUP
SIGINT
SIGQUIT
SIGILL
SIGTRAP
SIGABRT
SIGBUS
SIGFPE
SIGKILL
SIGUSR1
SIGSEGV
SIGUSR2
SIGPIPE
SIGALRM
SIGTERM
SIGSTKFLT
SIGCHLD
SIGCONT
SIGSTOP
SIGURG
SIGXCPU
SIGXFSZ
SIGVTALRM
SIGPROF
SIGWINCH
SIGIO
SIGPWR
SIGSYS
32
33
SIGRTMIN
SIGRTMIN+1
SIGRTMIN+2
SIGRTMIN+3
SIGRTMIN+4
SIGRTMIN+5
SIGRTMIN+6
SIGRTMIN+7
SIGRTMIN+8
SIGRTMIN+9
SIGRTMIN+10
SIGRTMIN+11
SIGRTMIN+12
SIGRTMIN+13
SIGRTMIN+14
SIGRTMIN+15
SIGRTMAX-14
SIGRTMAX-13
SIGRTMAX-12
SIGRTMAX-11
SIGRTMAX-10
SIGRTMAX-9
SIGRTMAX-8
SIGRTMAX-7
SIGRTMAX-6
SIGRTMAX-5
SIGRTMAX-4
SIGRTMAX-3
SIGRTMAX-2
SIGRTMAX-1
SIGRTMAX

HP-UX ksh does not have all of there defined but one of the should work on your system.

I executed all the above provided options but none gave me desired output:

$ trap 'PS1="$(date ) > "' EXIT
$ trap 'PS1="$(date ) > "' SIGHUP
$ trap 'PS1="$(date ) > "' SIGINT
$ trap 'PS1="$(date ) > "' SIGQUIT
$ trap 'PS1="$(date ) > "' SIGILL
$ trap 'PS1="$(date ) > "' SIGTRAP
$ trap 'PS1="$(date ) > "' SIGABRT
$ trap 'PS1="$(date ) > "' SIGBUS
$ trap 'PS1="$(date ) > "' SIGFPE
$ trap 'PS1="$(date ) > "' SIGKILL
$ trap 'PS1="$(date ) > "' SIGUSR1
$ trap 'PS1="$(date ) > "' SIGSEGV
$ trap 'PS1="$(date ) > "' SIGUSR2
$ trap 'PS1="$(date ) > "' SIGPIPE
$ trap 'PS1="$(date ) > "' SIGALRM
$ trap 'PS1="$(date ) > "' SIGTERM
$ trap 'PS1="$(date ) > "' SIGSTKFLT
$ trap 'PS1="$(date ) > "' SIGCHLD
$ trap 'PS1="$(date ) > "' SIGCONT
$ trap 'PS1="$(date ) > "' SIGSTOP
$ trap 'PS1="$(date ) > "' SIGURG
$ trap 'PS1="$(date ) > "' SIGXCPU
$ trap 'PS1="$(date ) > "' SIGXFSZ
$ trap 'PS1="$(date ) > "' SIGVTALRM
$ trap 'PS1="$(date ) > "' SIGPROF
$ trap 'PS1="$(date ) > "' SIGWINCH
$ trap 'PS1="$(date ) > "' SIGIO
$ trap 'PS1="$(date ) > "' SIGPWR
$ trap 'PS1="$(date ) > "' SIGSYS
$ trap 'PS1="$(date ) > "' 32
$ trap 'PS1="$(date ) > "' 33
$ trap 'PS1="$(date ) > "' SIGRTMIN
$ trap 'PS1="$(date ) > "' SIGRTMIN+1
$ trap 'PS1="$(date ) > "' SIGRTMIN+2
$ trap 'PS1="$(date ) > "' SIGRTMIN+3
$ trap 'PS1="$(date ) > "' SIGRTMIN+4
$ trap 'PS1="$(date ) > "' SIGRTMIN+5
$ trap 'PS1="$(date ) > "' SIGRTMIN+6
$ trap 'PS1="$(date ) > "' SIGRTMIN+7
$ trap 'PS1="$(date ) > "' SIGRTMIN+8
$ trap 'PS1="$(date ) > "' SIGRTMIN+9
$ trap 'PS1="$(date ) > "' SIGRTMIN+10
$ trap 'PS1="$(date ) > "' SIGRTMIN+11
$ trap 'PS1="$(date ) > "' SIGRTMIN+12
$ trap 'PS1="$(date ) > "' SIGRTMIN+13
$ trap 'PS1="$(date ) > "' SIGRTMIN+14
$ trap 'PS1="$(date ) > "' SIGRTMIN+15
$ trap 'PS1="$(date ) > "' SIGRTMAX-14
$ trap 'PS1="$(date ) > "' SIGRTMAX-13
$ trap 'PS1="$(date ) > "' SIGRTMAX-12
$ trap 'PS1="$(date ) > "' SIGRTMAX-11
$ trap 'PS1="$(date ) > "' SIGRTMAX-10
$ trap 'PS1="$(date ) > "' SIGRTMAX-9
$ trap 'PS1="$(date ) > "' SIGRTMAX-8
$ trap 'PS1="$(date ) > "' SIGRTMAX-7
$ trap 'PS1="$(date ) > "' SIGRTMAX-6
$ trap 'PS1="$(date ) > "' SIGRTMAX-5
$ trap 'PS1="$(date ) > "' SIGRTMAX-4
$ trap 'PS1="$(date ) > "' SIGRTMAX-3
$ trap 'PS1="$(date ) > "' SIGRTMAX-2
$ trap 'PS1="$(date ) > "' SIGRTMAX-1
$ trap 'PS1="$(date ) > "' SIGRTMAX
$

run:

a.ksh | sh

where a.ksh:

#!/bin/ksh
x=0
while [ $x -le 100 ]
do
  echo "trap 'PS1=\"\$(date ) > \"'" $x
  (( x = x + 1 ))
done

There will be an error like:

sh[49]: 48: The specified trap syntax is not correct.

But the trap for SIGCHLD should be set. The command prompt should be updating after commands.

Received following error instead:

$ a.ksh | sh
DEBUG : Max recursion reached it's limit, exiting
$

Your shell cannot loop to 100?!? change 100 to 75, and so on...

Try this: in file setprompt put:

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="$_timehms $ "

Then from your command line, source the setprompt file eg:

 $ . ./setprompt
09:25:27 $ 
09:25:38 $ 
1 Like

change a.ksh to:

#!/bin/ksh
x=0
while [ $x -le 65 ]
do
  trap 'PS1="$(date ) > "' $x
  (( x = x + 1 ))
done

just run:

a.ksh

There should be an error like:

./a.ksh[6]: 48: bad trap

I tried executing it with 75 it again gave me same error then I executed it with 50, it ran fine with no error on console but still not getting date in prompt.

$ a.ksh | sh
$
$ echo $SIGCHLD
 
$