Bourne Shell Question?

Doug Gwyn gwyn at smoke.BRL.MIL
Thu Feb 2 09:14:36 AEST 1989


In article <8517 at dasys1.UUCP> dap9702 at dasys1.UUCP (Dan Powers) writes:
>  Does anyone know how to get a prompt in Bourne Shell that contains the
>current working directory (pwd).

Not all Bourne shells have sufficient capabilities to do this.  I think
for the Korn shell one would use "alias" in a manner similar to your
Cshell example.  On UNIX System V Release 2 and later shells (i.e. those
supporting shell functions), you can define a function (unfortunately
not called "cd"; use "ch" instead, for example) that sets the shell
variable PS1 appropriately as it does the "cd").  For 8th or 9th Edition
UNIX shells and the BRL Bourne shell, you can use the "builtin" builtin
to redefine "cd", for example:

cd(){
	if [ $# -lt 1 ]
	then	builtin cd
	else	builtin cd "$1"
	fi
	if [ `pwd` = "$HOME" ]
	then	PS1='$ '
	else	PS1=`echo "$CWD" | sed "s!^$HOME!~!"`'$ '
	fi
}

(My actual "cd" definition is MUCH more elaborate than this.)

You can adapt the above to SVR2 shells by renaming the function and
removing the "builtin" keywords.



More information about the Comp.unix.questions mailing list