I have changed my .kshrc to show my prompt as follows: 511 /export/home/students/cninja $ where 511 is the command number and ../students/cninja is the current working directory.

Is it possible to show a shortened version of the working directory, the prompt then showing something like: 511 ~/cninja $ or 511 ../students/cninja $ or what have you?

This is what my line looks like in .kshrc:

PS1=' ! $PWD $'; export PS1

Update: I haven't been able to determine whether I have a ksh88 or ksh93 shell. Most of the echo *what have you* commands return a blank stare and a prompt from the shell, and echo ${.sh.version} returns /bin/ksh: ${.sh.version}: bad substitution.

  • What OS are you running? uname -a
    – jippie
    Oct 16, 2012 at 6:42
  • SunOS solaris 5.10 Generic_141445-09 i86pc i386 i86pc Oct 16, 2012 at 14:57

4 Answers 4


I thought the following would work, but it turned out to be a bash specific solution. Leaving this answer for reference purpose.

export PS1=' ! $( basename $PWD )'

Maybe it requires backtics instead of the $( ) construct.

  • I'm not sure what you mean by basename. Would that be /export? Oct 14, 2012 at 19:07
  • no, basename is a shell command that returns the last 'part' of the $PWD.
    – jippie
    Oct 14, 2012 at 19:15
  • OIC! I will try it again, then. Oct 14, 2012 at 19:18
  • It didn't work. I tried PS1=' ! $( basename $PWD )' and PS1=' ! ` basename $PWD ` ' Oct 14, 2012 at 19:24
  • My trick must be bash specific then. Bash and ksh are pretty much alike, but not identical.
    – jippie
    Oct 14, 2012 at 19:29

There exist a few implementations of ksh:

  • The original (or AT&T) one by David Korn with two major variants (ksh88 and ksh93) each one having different versions with a different feature set
  • pdksh (the public-domain Korn shell) with a few derivatives.
  • zsh. Zsh has a ksh emulation mode which it activates when called as ksh.

You may want to use zsh as your ksh, or even switch altogether to zsh which is a much more powerful tool as an interactive shell.

If you call zsh as ksh. You can have the prompt expansion you want by doing (add it to your $ENV file):

setopt PROMPT_PERCENT # restore zsh prompt expansion disabled by the ksh mode
PS1='%! %~$ '

Otherwise, if you do need to stick with the AT&T or PD ones, you could aproximate it with:

PS1='! $(pwd | sed "s,^$HOME,~,")$ '

Which would work for ~ but not for ~other-user.


$ echo ${.sh.version}
Version JM 93u+ 2012-02-29
$ PS1='! $(pwd | sed "s,^$HOME,~,")$ '
85 ~$ cd /tmp
86 /tmp$ cd
87 ~$

From a vague recollection from decades ago, it could be that ksh88 doesn't expand command substitutions inside $PS1. In that case, you could still use some trick like:

PS1='! ${tilde[0${one#${PWD##"$HOME"*}1}]}${PWD#"$HOME"}$ '

(which I've now verified works on all of ksh93u+ on Debian, ksh88i on Solaris, MirBSD ksh 40.9.20120630 (pdksh derivative) on Debian, and zsh 4.3.17 (when called as ksh) on Debian, so I'd be surprised if there was any ksh where it didn't work)

The idea being that if $PWD starts with $HOME, then ${PWD##"$HOME"*} is empty, so ${one#1} is empty, so we get ${tilde[0]} that is $tilde. But if it doesn't then ${PWD##"$HOME"*} is $PWD, then ${one#${PWD}1} is $one, so we get ${tilde[1]} which is empty as the $tilde array as only one element.

  • 1
    I may be missing something, but I tried PS1='! $(pwd | sed "s,^$HOME,~,")$ ' and it didn't work. Oct 15, 2012 at 16:19
  • In which way does it not work? What version/implementation of ksh? Oct 15, 2012 at 16:33
  • It changed the prompt to (pwd | sed "s,^$HOME,~,")$ ', basically. I get bad substitution with this echo ${.sh.version}. Is there another way to find the version? Sorry that my n00b is showing. Oct 15, 2012 at 16:50
  • It should be $(pwd...) (with $). What effect do you get? If it's pdksh, you may look at echo "$KSH_VERSION". Or you can try what /bin/ksh. Oct 15, 2012 at 17:08
  • It changed it to 667 $(pwd | sed s,^/export/home/students/cninja,~,)$ Oct 15, 2012 at 17:11

Use a Korn Shell "discipline function" (basically a getter/setter) to wrangle the path into the form you want:

function RELATIVE_PWD.get {
    if [ "${PWD:0:${#HOME}}" = "$HOME" ]
PS1='! ${RELATIVE_PWD} \$ '

Tested on ksh93. Not sure if it works on ksh88.

  • Didn't know about this construct. I only care about the current dir name, so my variation looks like this: function PS1PWD.get { [ "${PWD}" == "${HOME}" ] && .sh.value="~" || .sh.value="${PWD##*/}"; } and include ${PS1PWD} in my prompt.
    – Rich
    Aug 9, 2019 at 17:53
UserLoc=$(for user_loc in $(egrep -v "root|shutdown|halt|nologin" /etc/passwd | awk -F: '{print $1}' ); do echo -e "$user_loc"; done | grep $USER)

# User shell
MYSHELL=$(echo $SHELL | awk -F/ '{print $NF}')
if [ "$MYSHELL" == "bash" ] ; then
        Default=$(echo -e "\e[00m")
        Black=$(echo -e "\e[30m")
        Red=$(echo -e "\e[31m")
        Green=$(echo -e "\e[32m")
        Orange=$(echo -e "\e[33m")
        Blue=$(echo -e "\e[34m")
        Magenta=$(echo -e "\e[35m")
        Cyan=$(echo -e "\e[36m")
        White=$(echo -e "\e[37m")
        Def=$(echo -e "\e[0;0m")
        Bold=$(echo -e "\e[1m")
        Under=$(echo -e "\e[4m")
        Normal=$(tput sgr0)
        PS1='[${Color}${USER}${Default}@${Hostname}] \W ${PrtF} '
elif [ "$MYSHELL" == "ksh" ] ; then
        Default=$(echo -e "${esc}00m")
        Black=$(echo -e "${esc}30m")
        Red=$(echo -e "${esc}31m")
        Green=$(echo -e "${esc}32m")
        Orange=$(echo -e "${esc}33m")
        Blue=$(echo -e "${esc}34m")
        Magenta=$(echo -e "${esc}35m")
        Cyan=$(echo -e "${esc}36m")
        White=$(echo -e "${esc}37m")
        Def=$(echo -e "${esc}0m")
        Bold=$(echo -e "${esc}1m")
        Under=$(echo -e "${esc}4m")
        PS1='[${Color}${USER}${Default}@${Hostname}] $(if [ "${PWD#$HOME}" != "$PWD" ];then print "~${PWD#$HOME}"; else print "$(basename $PWD)"; fi) ${PrtF} '
        PS1="[\u@\h] \w \\$ "

# Color for login's user
# if root or admin user => Red
if [ "$EUID" -eq 0 ] ; then
# if user local => Cyan
elif [ "$UserLoc" == "$USER"  ] ; then
# if standard user (AD or LDAP) => green

export PS1

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.