3

On my AIX box, which is using ksh as the default shell, I'd like the prompt to show both the date and time followed by a newline \n, along with the name of the machine, and the working directory.

Something like:

2014/08/05 10:58:43
[username]machinename:/some/path/here $

I've tried the following:

unset _Y _M _D _h _m _s
eval $(date "+_Y=%Y;_M=%m;_D=%d;_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}'
_timedhms=$_Y'/'$_M'/'$_D" "'${_x[_tsub]}$_h:$_m:${_s}'

PS1="$_timedhms'\n' "'[USERNAME]MACHINE:${PWD#$HOME/} $ '

However, there is no newline between the date/time and the rest of the prompt. It seems now matter what the combination of quotes I try, I cannot get a newline to appear in PS1.

Edit: nixed the spaces after SECONDS assignment, and edits need to be 6 characters long, so I'm telling you here.

1
  • Double quoted strings in ksh do not automatically interpret \n as a newline. As Gnouc demonstrates there are workarounds. – chicks Aug 5 '14 at 17:30
3

You can use the literal newline in PS1:

PS1="$_timedhms
> [USERNAME]MACHINE:${PWD#$HOME/} $ "

or using $'\n' with ksh93:

PS1="$_timedhms$'\n' [USERNAME]MACHINE:${PWD#$HOME/} $ "
2
  • The literal newline works, however the $'\n' does not appear to work at all. I just get an n displayed inline. – Hannah Vernon Aug 5 '14 at 17:37
  • It seems only work with ksh93. Updated my answer. – cuonglm Aug 5 '14 at 17:48
-1

There is pretty simple way to do this:

export PS1=$(echo "\033[01;33m"`date +%D`" "`date +%T`"\033[0m""\n"`whoami`@"\033[3;36m"`hostname`"\033[0m"':$PWD'#)

If you want to set this permanently add the entry to '/etc/profile'.

2
  • Or ~/.profile is you don't want to force it on everyone – Jeff Schaller Sep 24 '16 at 0:37
  • Speaking of permanently — this sets the prompt to permanently/perpetually display the current date/time (i.e., the instant when you login, or otherwise set the prompt string), rather than the date/time when the prompt is issued. – G-Man Says 'Reinstate Monica' Sep 24 '16 at 0:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.