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.

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.

share|improve this question
    
What OS are you running? uname -a –  jippie Oct 16 '12 at 6:42
    
SunOS solaris 5.10 Generic_141445-09 i86pc i386 i86pc –  cornbread ninja Oct 16 '12 at 14:57

2 Answers 2

up vote 1 down vote accepted

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.

Example:

$ 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:

one=1
tilde='~'
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.

share|improve this answer
    
I may be missing something, but I tried PS1='! $(pwd | sed "s,^$HOME,~,")$ ' and it didn't work. –  cornbread ninja Oct 15 '12 at 16:19
    
In which way does it not work? What version/implementation of ksh? –  Stéphane Chazelas Oct 15 '12 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. –  cornbread ninja Oct 15 '12 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. –  Stéphane Chazelas Oct 15 '12 at 17:08
    
It changed it to 667 $(pwd | sed s,^/export/home/students/cninja,~,)$ –  cornbread ninja Oct 15 '12 at 17:11

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.

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

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.