[comp.unix.questions] Getting UNIX prompt to display current directory

fristens@ncsugn.ncsu.edu (Brian Fristensky) (03/19/89)

In MS-DOS there is a very easy way to get the current directory displayed as
part of the prompt  (prompt $p).  In UNIX, there is certainly no
straightforward way to do this.  Can anyone think of a tricky way?  My
guess is that it would involve getting pwd output into the shell variable
$PS1, but that would require somehow getting pwd to execute after each
command you type. 

Your prompt assistance in this matter will be appreciated.
(sorry.)

Brian Fristensky
Dept. of Botany
North Carolina State University

nscugn!fristens@mcnc.uucp

ilan343@violet.berkeley.edu (03/19/89)

In article <5582@ncsugn.ncsu.edu> fristens@ncsugn.ncsu.edu (Brian Fristensky) writes:
>In MS-DOS there is a very easy way to get the current directory displayed as
>part of the prompt  (prompt $p).  In UNIX, there is certainly no
>straightforward way to do this.  Can anyone think of a tricky way?  My
>guess is that it would involve getting pwd output into the shell variable
>$PS1, but that would require somehow getting pwd to execute after each
>command you type. 

I use an idea plagiarized from a contribution by Daniel Bauer to the
Wizard's Grabbag (UnixWorld-August 1988). It creates a function to
replace "cd".  Everytime you change directory, PS1 is redefined.
It also changes the prompt whenever a sub-shell is open, keeping count
of what level your are.

news@lindy.Stanford.EDU (News Service) (03/19/89)

) In MS-DOS there is a very easy way to get the current directory displayed as
) part of the prompt  (prompt $p).  In UNIX, there is certainly no
) straightforward way to do this.  Can anyone think of a tricky way? 

My favorite answer to this perennial question:

PS1=.

"I tried that and it didn't work ..."

Call for votes for comp.unix.newusers, anyone?


In shells that allow aliases or functions, the usual trick is to
redefine the commands that let you change your current directory
(cd, pushd, popd, ...) so they also change $PS1 or $prompt or
whatever.  If I'm not mistaken, ksh also allows a better way to do
this.  In any case, a year's worth of archive of this newsgroup would
doubtless turn up a "wealth" of responses, for any given year.

brendan@jolnet.ORPK.IL.US (Brendan Kehoe) (03/19/89)

In article <5582@ncsugn.ncsu.edu> fristens@ncsugn.ncsu.edu (Brian Fristensky) writes:
>                                 Can anyone think of a tricky way?  My
>guess is that it would involve getting pwd output into the shell variable
>$PS1, but that would require somehow getting pwd to execute after each
>command you type. 

 I have this set up in my .kshrc file:

PS1='${PWD##} $LOGNAME>'

 This will give you (for example): /usr/usr1/brendan brendan>
as a prompt. It does *not* work in the Bourne shell..only Korn.

>Brian Fristensky
>Dept. of Botany
>North Carolina State University
>
>nscugn!fristens@mcnc.uucp


-- 
Brendan Kehoe
brendan@cup.portal.com    | GEnie: B.KEHOE  | Oh no! I forgot to say goodbye
brendan@chinet.chi.il.us  | CI$: 71750,2501 |  to my mind!
brendan@jolnet.orpk.il.us | Galaxy: Brendan |                - Abby Normal

gwyn@smoke.BRL.MIL (Doug Gwyn ) (03/19/89)

In article <5582@ncsugn.ncsu.edu> fristens@ncsugn.ncsu.edu (Brian Fristensky) writes:
>In MS-DOS there is a very easy way to get the current directory displayed as
>part of the prompt  (prompt $p).  In UNIX, there is certainly no
>straightforward way to do this.

False.

>My guess is that it would involve getting pwd output into the shell variable
>$PS1, but that would require somehow getting pwd to execute after each
>command you type. 

False.

This question arises every six months or so.  If your shell supports
functions or aliases, then you can replace the "cd" command with one
that not only chdirs but also sets $PS1 or $prompt (depending on the
type of shell).  If your shell supports evaluation of the prompt
string, then there is an obvious way to accomplish what you want.
Finally, if you have nothing but incredibly puny shells, you can set
a trap in your interactive shell that modifies the prompt string
when a signal is sent by a "cd"-like executable binary or shell script.
Depending on the shell, you may have to use some command other than
"cd" when changing working directories in order for the kludgery to
work.

gandalf@csli.STANFORD.EDU (Juergen Wagner) (03/19/89)

Ok, guys. It's prompt time again.

The answers to the question are:
(1)  It depends on your shell (csh, sh, tcsh, ksh, ...).
(2)  The trick is not to update the prompt after every command but only
     after commands changing the current working directory. In csh, you
     can alias cd, pushd, popd, etc., so they set the csh variable "prompt"
     properly. In tcsh, you can specify something like
	set prompt = "${HOSTNAME}:%d(%h)${dot} "
     to include the proper working dir in the prompt. Your mileage may vary.
     It should be all in TFM for your favourite shell.
(3)  This question arises every 6 months or so, and promptly causes
     prompt wars.
(4)  Typically about a month after the original posting, after all variations
     have been stated, somebody asks for a repost.

Enjoy,
-- 
Juergen Wagner		   			gandalf@csli.stanford.edu
						 wagner@arisia.xerox.com

samlb@pioneer.arc.nasa.gov (Sam Bassett RCD) (03/20/89)

:-)  Maybe we should petition the net-gods for a newsgroup named
'comp.prompt'  :-)  :-)


Sam'l Bassett, Sterling Software @ NASA Ames Research Center, 
Moffett Field CA 94035 Work: (415) 694-4792;  Home: (415) 454-7282
samlb%well@lll-crg.ARPA samlb@pioneer.arc.nasa.gov 
<Standard Disclaimer> := 'Sterling doesn't _have_ opinions -- much less NASA!'

derek@hsi.UUCP (Derek Lee-Wo) (03/20/89)

In article <5582@ncsugn.ncsu.edu> fristens@ncsugn.ncsu.edu (Brian Fristensky) writes:
>In MS-DOS there is a very easy way to get the current directory displayed as
>part of the prompt  (prompt $p).  In UNIX, there is certainly no
>straightforward way to do this.  Can anyone think of a tricky way?  My

This works only in BSD.
	alias cd 'cd \!*;set prompt="[`/bin/pwd`] "'

-- 
+-----------------------------------------------------------------------------+
|Derek Lee-Wo, Health Systems International, New Haven, CT 06511.             |
|E-mail address :- derek@hsi.com      ...!yale!hsi!derek                      |
+-----------------------------------------------------------------------------+

derek@hsi.UUCP (Derek Lee-Wo) (03/20/89)

In article <341@hsi86.hsi.UUCP> derek@hsi86.UUCP (Derek Lee-Wo) writes:
>In article <5582@ncsugn.ncsu.edu> fristens@ncsugn.ncsu.edu (Brian Fristensky) writes:
>>In MS-DOS there is a very easy way to get the current directory displayed as
>>part of the prompt  (prompt $p).  In UNIX, there is certainly no
>>straightforward way to do this.  Can anyone think of a tricky way?  My
:
:This works only in BSD.
    +-------------->^^^^
:   |	alias cd 'cd \!*;set prompt="[`/bin/pwd`] "'
:   |
    |
Sorry, I meant to say csh in my previous posting.
-- 
+-----------------------------------------------------------------------------+
|Derek Lee-Wo, Health Systems International, New Haven, CT 06511.             |
|E-mail address :- derek@hsi.com      ...!yale!hsi!derek                      |
+-----------------------------------------------------------------------------+

gilbert@necbsd.NEC.COM (Russell Gilbert) (03/21/89)

From article <2455@lindy.Stanford.EDU>, by news@lindy.Stanford.EDU (News Service):
> ) In MS-DOS there is a very easy way to get the current directory displayed as
> ) part of the prompt  (prompt $p).  In UNIX, there is certainly no
> ) straightforward way to do this.  Can anyone think of a tricky way? 

In K-shell, in my .profile, I use:     PS1='$PWD> '

In C-shell, in my .login, I use:

   if ($?prompt) then
      set X=`pwd`
      set prompt="$X>C> "
   alias cd 'cd \!*; set X=`pwd`; set prompt = "$X> "'

RG

gandalf@csli.STANFORD.EDU (Juergen Wagner) (03/21/89)

In article <1673@amelia.nas.nasa.gov> samlb@pioneer.arc.nasa.gov.UUCP
(Sam Bassett RCD) writes:
>:-)  Maybe we should petition the net-gods for a newsgroup named
>'comp.prompt'  :-)  :-)

Actually, a newsgroup comp.unix.newusers wouldn't be a bad idea to catch
all the recurring topics such as prompts, rm flags, aliases, NULL or not
NULL, etc.

-- 
Juergen Wagner		   			gandalf@csli.stanford.edu
						 wagner@arisia.xerox.com

maart@cs.vu.nl (Maarten Litmaath) (03/21/89)

fristens@ncsugn.ncsu.edu (Brian Fristensky) writes:
\... it would involve getting pwd output into the shell variable
\$PS1, ...

It seems you're using /bin/sh (or ksh). The following will work:

	Solution 1 (put the following in a script `c').
	------------------------------
	#! /bin/sh
	#
	# c: a script to change the prompt of the parent to the new working
	# directory
	# to put in $HOME/.profile:
	#
	#    PARENT=$$
	#    export PARENT
	#    trap 'DIR=`cat $HOME/.dir`; cd $DIR; PS1="$DIR> "' 5

	cd $1
	pwd > $HOME/.dir
	exec kill -5 $PARENT
	------------------------------

	Solution 2 (if your sh has shell functions).
	------------------------------
	c()     # you might not be able to name this function `cd'
	{
		cd $1
+		PS1="`pwd`> "
	}
	------------------------------

	Solution 3 (if your sh has both shell functions and $PWD).
	------------------------------
	Change the line indicated with `+' to

		PS1="$PWD> "
-- 
 Modeless editors and strong typing:   |Maarten Litmaath @ VU Amsterdam:
   both for people with weak memories. |maart@cs.vu.nl, mcvax!botter!maart

blume@netmbx.UUCP (Heiko Blume) (03/22/89)

this sure is sort of ugly, but it works. you'll get a prompt like
user@host tty2A /usr/yourdir {23} 
if you put this in .login (NOT .cshrc or you'll get the prompt if you
fork a shell from another prgm. if you want the prompt in subshells too
there is another baad method.....) 
btw i threw in PushDir SwapDir FlipDir stack and POPdir.

alias a alias
if (! $?_d) set _d = ()
a pop	'cd $_d[1]; shift _d'
a pd	'set _d = (`pwd` $_d); cd \!*'
a sd	'set _d = ($_d[2] $_d[1] $_d[3-])'
a fd	'pd .; sd ; pop'
a stack 'echo $_d'
endif
umask 007
set x=`who am i` 
set me="$x[1]@`uname -n` $x[2]"
setenv me "$me"
a cd 'chdir \!*; set prompt="$me `pwd` {\\!} "'
set prompt="$me `pwd` {\!} "
-- 
Heiko Blume,Seekorso 29,D-1000 Berlin 22,VOICE=(+49 30)365 55 71,BBS=()365 75 01
TELEX=183003 intro d,FAX=()882 50 65|ARPA  =crash!pnet01!pro-cess!blume@nosc.mil
PSI  =PSI%45300043109::netmbx:blume |BITNET=pro-cess.UUCP!blume@PSUVAX1
UUCP =blume@netmbx.UUCP             |INET  =blume@pro-cess.cts.com

shagar@vpk4.UUCP (Steven Hagar) (03/22/89)

The one thing that I didn't like about putting the current directory in PS1
was that if I was in something like "/usr/spool/uucppublic/receive/..."
there wasn't much space left on the line to see the command I was typing. 
An alternate approach that I found useful was to display the current
directory in the status field of my terminal.  Here are the functions
that I use within Bourne and Korn shells on AT&T terminals:

case ${TERM} in

605|610|615)
pd(){
cd $1
msg=`uname`": "`pwd`
len=`expr 80 - "${msg}" : '.*'`
sp="                    "
echo "^[7\033[x${sp}${sp}${sp}${sp}\c"
echo "\033[25;${len}x${msg}^[8\c"
};;

630)
pd(){
cd $1
msg=`uname`": "`pwd`
len=`expr "${msg}" : '.*'`
echo "\033[?${len};2v${msg}\c"
};;

esac


(suggestions for removing nasty terminal dependencies welcomed --
 please send comments or questions via e-mail)
-- 

  Steven Hagar             .
  AT&T Canada Inc.         .  ATTMAIL: !shagar
  (416) 756-5061           .  UUCP: uunet!attcan!shagar

connet@sal50.usc.edu (David Connet) (03/22/89)

In article <2187@solo11.cs.vu.nl> maart@cs.vu.nl (Maarten Litmaath) writes:
.	Solution 2 (if your sh has shell functions).
.	------------------------------
.	c()     # you might not be able to name this function `cd'
.	{
.		cd $1
.+		PS1="`pwd`> "
.	}
.	------------------------------
.
.	Solution 3 (if your sh has both shell functions and $PWD).
.	------------------------------
.	Change the line indicated with `+' to
.		PS1="$PWD> "

Actually you can just get away with
	PS1="\$PWD: "
        ^
The backslash is required.  The shell (at least ksh) then reevaluates
the PS1 each time.

vwa0201@marst2.UUCP (Larry Baca) (03/25/89)

In article <87@necbsd.NEC.COM>, gilbert@necbsd.NEC.COM (Russell Gilbert) writes:
> From article <2455@lindy.Stanford.EDU>, by news@lindy.Stanford.EDU (News Service):
> > ) In MS-DOS there is a very easy way to get the current directory displayed as
> > ) part of the prompt  (prompt $p).  In UNIX, there is certainly no
> > ) straightforward way to do this.  Can anyone think of a tricky way? 

In T-SHELL, in my .cshrc, I use:

     set prompt="%S%M%s [%h] %"

This gives me, on the first line, my current dir.
On the next line, my machine name (we have 2), followed by the current
command no.
-- 
LARRY BACA, SYSTEM ADMINISTRATOR, [4m(MARST1 MARST2)[0m
DAASO-VWA AIS, DEFENSE AUTOMATIC ADDRESSING OFFICE, WESTERN DIVISION
DDTC TRACY, TRACY CA. 95376-5057
AUTOVON 462-9391, COMERCIAL 832-9391    EMAIL PATH - dsacg1!marst2!vwa0201