Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I have been trying to get the PS1 into a personal format, I use Korn shell.
What I am trying is for the PS1 to only show the last two directories.
I got that right, the problem is that once I set the PS1 with it, it keeps showing the first directory.# pwd
/usr/users/plaj
# pwd | awk -F/ '{x= NF-1 ; if (x >1){ print "/"$x"/"$NF} else {print}}'
/users/plaj
# PS1=`pwd | awk -F/ '{x= NF-1 ; if (x >1){ print "/"$x"/"$NF} else {print}}'`"# "
/users/plaj# cd /
/users/plaj#

Sorry, but the PS1 doesn't support the syntax you are using. As you've described, you can set the initial PS1 value with the awk command you've described. The only way that PS1 can change dynamically is to use the internal shell variables. For example, the PWD variable is populated by the results of the cd command. Therefore this command:
export PS1='$PWD'" "
displays the current directory after the cd command completes. Unfortunately, PS1 doesn't allow you to allow external unix commands after the initial setting.
You might find this link interesting:
http://www.itworld.com/Comp/2378/swol-062300-unix101/

Thanks for the explanation, it pushed me to try the following, but offcourse it didn't work neither, the only other thing I found was trying with putenv(), but that will require to do some C programming and compile it, and I will probably mess more than I want to.
The only thing is that I didn't quite understood why the SECONDS env variable did work for the time part of the script(prompt)#Including a Time Prompt
export SECONDS="$(date '+3600*%H+60*%M+%S')"
typeset -Z2 _h _m _s
_hh="(SECONDS/3600)%24"
_mm="(SECONDS/60)%60"
_ss="(SECONDS%60)"
_time='${_x[(_m=_mm)==(_h=_hh)==(_s=_ss)]}$_h:$_m:$_s'
#Prompt
export PWD="$(pwd | awk -F/ '{x= NF-1 ; if (x >1){ print "/"$x"/"$NF}}')"
PS1=$_time'${PWD}#'

I shared this problem with a friend of mine. While normal unix tools won't work with PWD when setting PS1, ksh patterns will. This seems to work:
export PS1='${PWD#${PWD%/*/*}} >'
However, it breaks if you cd to a subdirector with one or less parents such as tmp or /

![]() |
If statement exception
|
BigBrother and Hobbit Ser...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |