PWD in Bourne Shell PS1

Chris Hare / System Manager chris at choreo.COM.COM
Sun Jan 7 06:40:49 AEST 1990


In article <8790.259b3142 at ecs.umass.edu> Satam writes,
> Inserting the following three aliases will help achieve the same
> effect. Note that this is only for csh.
> ------------------------------------------------------------
> alias cd 'chdir \!* && set prompt="${cwd:t}_\\!% "'
> alias pushd 'pushd \!* && set prompt="${cwd:t}_\\!% "'
> alias popd 'popd \!* && set prompt="${cwd:t}_\\!% "'
> -------------------------------------------------------------

This is quite true.  However, a similar thing can be implemented in the
Bourne shell using shell functions.  There are however two caveats :

	1.  We cannot use the name of a built in shell command 
	2.  We must recaculate the PS1 each time we do a cd.

Firstly,  becase we want the PS1 to be updated whenever we change
directories, we cannot use the builtin 'cd' command, but something like it.
We must also remember to use the new command, because using a cd will mean
that the directory reflected in our PS1 will be incorrect.

You can try this Bourne shell function :

	go()
	   {
	   if [ "$1" ]		# if we got an argument
	   then
	      cd $1
	   else 		# otherwise go home
	      cd $HOME
	   fi
	   PS1=`pwd`"> "	# change the PS1
	   }

Remember, shell functions are not passed to their subshells.

Chris Hare				Choreo Systems Inc.
Coordinator, Systems Management		150 Laurier Ave West
Authorized SCO Instructor		Ottawa, Canada
					Phone 613-238-1050
					Fax   613-238-4453
*** It Works for ME ! ***		email : uunet!choreo!chris



More information about the Comp.unix.wizards mailing list