Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
    
Double quoted strings in ksh do not automatically interpret \n as a newline. As Gnouc demonstrates there are workarounds. –  chicks Aug 5 at 17:30

1 Answer 1

up vote 3 down vote accepted

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/} $ "
share|improve this answer
    
The literal newline works, however the $'\n' does not appear to work at all. I just get an n displayed inline. –  Max Vernon Aug 5 at 17:37
    
It seems only work with ksh93. Updated my answer. –  cuonglm Aug 5 at 17:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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