Two-line prompt using Korn

I'm attempting to set up a two-line prompt using Korn.

This is what I've set up in .kshrc

PS1='$(print -n "`logname`@`hostname`:";if [[ "${PWD#$HOME}" != "$PWD" ]] then; print -n "~${PWD#$HOME}"; else; print -n "$PWD";fi;print "\n$ ")'

And in .profile,

ENV="$HOME/.kshrc"; export ENV

The hosts that in use are either running RHEL kernel 2.6.8-xx with ksh88 or 2.6.9-xx with ksh93
This works perfectly for most hosts and I get the desired output:

userx@hosty:~
$ 
userx@hosty:~
$ cd Documents/
userx@hosty:~/Documents
$

But on some hosts I get this error when I login:

-ksh: syntax error: `;' unexpected
$(print -n "`logname`@`hostname`:";if [[ "${PWD#$HOME}" 1074 "$PWD" ]] then; pr
int -n "~${PWD#$HOME}"; else; print -n "$PWD";fi;print "\n$ ")

I also noticed that != changes to a number (1074 in this example) and increases by 1 if I hit Enter.

I'm using the same syntax on all hosts and cannot figure out this error.
My guess is the difference in the ksh version.

Please help me figure out what I'm doing wrong.
Also, is there a way to force ksh to behave consistently across older versions?

Get the version of ksh you are using echo $KSH_VERSION and change all print statements to echo and see if that'd help...

1 Like

Did you also notice the line break in the middle of print shown in red above?

Are you sure that .kshrc has the same contents on both systems?

echo $KSH_VERSION throws a blank line,
I use the vi mode, so an ESC and a CTRL +V gives me the version.
For simplicity, I'll narrow it down to the 93 versions and skip the 88 as host with the older versions are miminal.

I have a feeling its not the KSH version thats causing this.

The prompt works perfectly on:

Version AJM 93t+ 2010-02-02

It fails on

Version M 1993-12-28

Works well on some fails on others with:

Version AJM 93t+ 2010-06-21

On the hosts on which it failed, replacing print with echo ignores the newline character, throwing this output:

userx@hosty:~\n$

I'm sorry that line break is actually the STDOUT on the terminal. I should have corrected it before pasting it here.
The .kshrc has no line breaks and is consistent on all hosts.

Well that's pretty close to what you want, why not now replace:

echo "\n$ "

with

echo;echo "$ "

No luck

ksh93: syntax error: `end of file' unexpected
$(echo -n "`logname`@`hostname`:";if [[ "${PWD#$HOME}" 669= "$PWD" ]] then; echo -n "~${PWD#$HOME}"; else; echo -n "$PWD";fi;echo;echo "$ "

Missing the close bracket

PS1='$(echo -n "`logname`@`hostname`:";if [[ "${PWD#$HOME}" 669= "$PWD" ]] then; echo -n "~${PWD#$HOME}"; else; echo -n "$PWD";fi;echo;echo "$ ")'
1 Like

Same error as before...

-ksh: line 1: syntax error: `;' unexpected
$(echo -n "`logname`@`hostname`:";if [[ "${PWD#$HOME}" 1002 "$PWD" ]] then; echo -n "~${PWD#$HOME}"; else; echo -n "$PWD";fi;echo;echo "$ ")

But this now ONLY fails on hosts with Version M 1993-12-28

It works on the hosts with Version AJM 93t+ 2010-06-21.

Try the following:

PS1='$(printf "%s@%s:" "$(logname)" "$(hostname)";[ "${PWD#$HOME}" == "$PWD" ] || printf "~";printf "%s\n$ " "${PWD#$HOME}")'

Note that I reversed some of the logic so I could use "==" instead of "!=". I use set -o vi , but I have heard of some cases where set -o emacs sometimes invokes the command history mechanism when != is used. If that is part of your problem (i.e. the 1002 in red above), this change should avoid it. I used printf instead of echo -n because it is more portable. (Using echo -n abc in ksh on a Solaris system (and some other UNIX branded systems) should print -n abc followed by a newline rather than abc without a newline that you would get with ksh on some other systems.

1 Like

Beautiful! Thank you... that worked perfectly on every host that failed earlier.
And thanks again for the useful tips. Much appreciated!

After closer inspection your command line does have unwanted/missing semicolons (shown in red above) so try the one below...

PS1=$(print -n "`logname`@`hostname`:";if [[ "${PWD#$HOME}" != "$PWD" ]]; then print -n "~${PWD#$HOME}"; else print -n "$PWD";fi;print "\n$ ")

which should work on all ksh versions...

1 Like