[comp.unix.questions] `pwd` in Shell prompt

H235-017%IRLEARN.BITNET@cunyvm.cuny.edu (Mark Humphrys) (04/01/88)

Calling Unix hackers,

  Forgive me if this is an absurdly easy question but I am pretty new to Unix.
I want to get the current directory to be automatically included in the Shell
prompt.
  Now
          $  PS1="`pwd` > "
works, but you have to type this in every time you change directory - not much
use. Alas, in my naivety, I thought that the following program would change
directory for me and change the prompt at the same time :

             cd $1
             PS1="`pwd` > "
             export PS1

but it does absolutely nothing. The reason appears to be that a subprocess
cannot alter its parent process.

Is there any way around this? How does the Unix programmer/hacker survive
without having a constant reminder of what directory he or she is in?

Again, forgive me if this question is ridiculously easy. You can reply to me
in person if you want, and we can have a good laugh about it.


Mark Humphrys
h235-017 at Irlearn
" Humanity I love you because when you're hard up you pawn your
intelligence to buy a drink."

parrish@nadc.arpa (D. Parrish) (04/01/88)

Mark,

   You're gonna get a lot of abuse.  This question has been
asked on this mailing list three or four times already.  But
anyhow, Dave asked it last and he sent me the information he
got, so now I'll forward it to  you.  Hope this helps.

Denice
parrish@nadc.arpa


	From Dave_B._Mensing.HENR801c@Xerox.COM Thu Feb 11 14:03:04 1988
	
	
	
	Below you will find a summary of the 40+ responses I received.
	
	KSH:
		To set your PS1 variable to show the current path:
			PS1='$PWD '
	
		You can also try which removes the HOME directory from the prompt when I am in
	a subdirectory of my HOME directory.
			PS1='${PWD#$HOME/}: '
	
	
	CSH:
		To set your prompt variable to show the current path:
			set prompt="`pwd`"
			alias setprompt 'set prompt="`pwd`"'
			alias cd 'cd \!* ; setprompt'
			alias pd 'pushd \!* ; setprompt'
			alias popd 'popd \!* ; setprompt'
	
		You need to ensure that after any command which can change your directory, you
	reset the prompt.
	
	If you need more info please message me and I will try to help.
	
	
	
	I forgot to mention the following:
	
	KSH:	Insert the PS1=... stuff into your '.profile' file.
	CSH:	Insert the prompt=... stuff into your '.cshrc' file.
	
	Sorry about that!
	
		Dave Mensing
	
		mensing.henr801c@xerox.com
		(716) 427-6423
	

gwyn@brl-smoke.ARPA (Doug Gwyn ) (04/01/88)

In article <12747@brl-adm.ARPA> H235-017%IRLEARN.BITNET@cunyvm.cuny.edu (Mark Humphrys) writes:
>I want to get the current directory to be automatically included in the Shell
>prompt.

Not again!  This was beaten to death a few months ago.
The answer is, a "vanilla" Bourne shell cannot update the PS1
string before every prompt (but the Korn shell can).  A Bourne
shell that does not support shell functions (e.g. the one shipped
with Berkeley UNIX) cannot update the PS1 (prompt) string as a
side-effect of executing a command to change directories, but
shells with shell functions can.  The vanilla SVR2 Bourne shell
does not permit shell functions to have the same names as
builtins, so such a command would have to be called something
other than "cd" (e.g. "ch").  The BRL Bourne shell, like the 8th
Edition UNIX shell that inspired some of its features, permits
builtins to be displaced by shell functions, so for example my
"cd" either updates the PS1 string on simple terminals or updates
the window banner on fancy terminals (e.g. AT&T 630 or Sun).

By the way, most people simply type "pwd" when they need to know
what their current working directory is.

winterss@psu-cs.UUCP (Stafford Winters ) (04/04/88)

In article <12752@brl-adm.ARPA> parrish@nadc.arpa (D. Parrish) writes:
>	CSH:
>		To set your prompt variable to show the current path:
>			set prompt="`pwd`"
>			alias setprompt 'set prompt="`pwd`"'
>			alias cd 'cd \!* ; setprompt'
>			alias pd 'pushd \!* ; setprompt'
>			alias popd 'popd \!* ; setprompt'
>	
   I suggest using the string that is already calculated for you:
            set prompt="$cwd"
            alias cd 'cd \!* ; set prompt="$cwd"'
            alias pushd 'pushd \!* ; set prompt="$cwd"'
   etc.
   I find it helpful to highlight my directory as well.  If you always use
the same type of terminal, then you can insert the appropriate control
sequences to do the highlighting.
>	
>	CSH:	Insert the prompt=... stuff into your '.cshrc' file.
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  ______
    /        /
 --/ __.  __/
(_/ (_/|_(_/_     Stafford `Tad' Winters
Internet: winterss%psu-cs.cs.pdx.edu      or     winters%jacobs.cs.orst.edu
UUCP: {ihnp4,tektronix}!{psueea!psu-cs!winterss,orstcs!jacobs!winters}
BITNET (for some): IN%"winters%jacobs.cs.orst.edu"

des@uucsbb.UUCP (Don Shope) (04/04/88)

In article <12747@brl-adm.ARPA> H235-017%IRLEARN.BITNET@cunyvm.cuny.edu (Mark Humphrys) writes:
>...
>I want to get the current directory to be automatically included in the Shell
>prompt.


The easiest way I have found of accomplishing this is by using a
Bourne shell "function". The function can be defined in your .profile.
Below is a sample fragment of my .profile. The function is called "c".
Use it exactly as you would use the "cd" shell command, with one 
addition: The syntax "c -" will switch back to the previous directory
that you were in. Have fun.

----------------- cut here ----------- .profile fragment ----------
export PS1 ODIR
ODIR=$HOME

# function to put current directory as part of prompt. It also
# "remembers" the previous directory, and goes there via "c -". This
# version also displays the system name as part of the prompt, which is
# useful in a networking environment.
c () 
  {
  if [ "$1" = "-" ]; then
     DIR=$ODIR
  else
     DIR=$1
  fi
  ODIR=`pwd`
  cd $DIR 
  PS1=`uuname -l`"!"`pwd`"> "
  }
c       # set the prompt now

------------------ end of cut -------------------------------------

alex@aiva.ed.ac.uk (Alex Zbyslaw) (04/08/88)

In article <7603@brl-smoke.ARPA> gwyn@brl.arpa (Doug Gwyn (VLD/VMB) <gwyn>) writes:
>>In article <12747@brl-adm.ARPA> H235-017%IRLEARN.BITNET@cunyvm.cuny.edu (Mark Humphrys) writes:
>>I want to get the current directory to be automatically included in the Shell
>>prompt.
>
>Not again!  This was beaten to death a few months ago.

Didn't you notice the date, Doug?  Can you say "April Fool"?  C'mon, the
sarcasm is just oozing off the original article; it just cannot be serious.

--Alex
-- 

     JANET:  alex@uk.ac.ed.eusip	ARPA:   alex%ed.eusip@nss.cs.ucl
	    UUCP:   ...{uunet, decvax, ihnp4}!mcvax!ukc!eusip!alex
        [CSNET BITNET]:  alex%ed.eusip%nss.cs.ucl@[csnet-relay cunyvm]

			Who needs opinions anyway?