Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

SUMMARY: Tricky ksh prompt -- parameter substitution

0 views
Skip to first unread message

H ZHU

unread,
Jun 28, 1994, 8:20:34 AM6/28/94
to
I received a lot of replies to my question, some in great detail.
Thanks to everybody replied. Excerpts attached at the end.

Here's the final solution to my problem, which I wrote drawing ideas
from several replies. The basic idea is to use shell function.
A single parameter substitution is not powerful enough.

----------------------------------------------------------
# ksh, setting prompt to relative path to $HOME if in subdir of $HOME,
# and to absolute path otherwise. Prefixed with a given string.
LEADER="" # whatever fixed string you want in prompt, eg. hostname.
cdfn(){
"cd" $1 &&
{ PS1="${PWD#$HOME}$ " &&
[[ $PWD = $HOME* ]] && PS1=${PS1#/}
PS1=$LEADER$PS1
}
}
alias cd='cdfn'
--------------------------------------------------------------

Following are some excerpts from the replies I received.
===============================================
From zh...@svnelson.ubs.ubs.ch (Patrick Seemann)
# shell functions
cdfunc() {
"cd" $1
PS1="`echo $PWD|sed s%$HOME%~%`> "
}
alias cd='cdfunc'
================================================
From sp...@cs.purdue.edu (Gene Spafford)
# shell functions
cdfunc() {
"cd" $1 && {
PS1="$PWD> "
[[ $PWD = $HOME* ]] && PS1="~${PWD#$HOME}> "
}
}
alias cd='cdfunc'
==================================================
From arp...@cats.ucsc.edu (Adam Paul)
cd () {
builtin cd $1
TMP=${PWD#$HOME}
if (test ${#TMP} != ${#PWD}); then
PS1='~$TMP> '
else PS1='$PWD> '
fi
}
===================================================
From: Grant Denkinson <gw...@geog.nott.ac.uk>
I haven't done this in ksh, nearest I have is in bash or csh

bash:
PROMPT_COMMAND="PS1='\h:\w bash> '"

csh:
alias stpt set cprom='`hostname``pwd|sed -e "s+/+:/+;s/:.usr.users.gwd10/:~/"`'
alias doprompt 'stpt && set prompt="$cprom csh> "'
alias cd 'chdir \!* && doprompt'
doprompt
alias pd 'pushd \!* && doprompt'
alias po 'popd \!* && doprompt'

I need to put the $HOME string in manually at the moment though:
/usr/users/gwd10
for the csh version.
=====================================================
From: d...@woodstock.abbott.com (Donald L. Ritchey)
[ ... a lengthy ksh function ...]
================================================
From Roma...@mch.sni.de (Roman Mey)
PS1="\${PWD#$HOME/} > ";export PS1
================================================

Thanks also to:
tpor...@netcom.com (Tony Porczyk)
spc...@armory.com (John DuBois)

---
Huaiyu Zhu zh...@aston.ac.uk
Dept of Computer Sciences and Applied Mathematics
Aston University, Birmingham B4 7ET, UK

0 new messages