[comp.unix.questions] current pwd in prompt

Mensing.HENR801c@Xerox.COM (02/06/88)

Can anyone explain to me how to get my prompt to be the current directory? I
have tried several methods without any success. It seems that no matter what
method I try, the prompt is evaluated once and never changes.

Just in case it matters, I am on an AT&T 3B2/400 running korn shell under UNIX
System V.3.1.

Thanks,
	Dave Mensing

	mensing.henr801c@xerox.com
	(716) 427-6423

lenny@icus.UUCP (Lenny Tropiano) (02/07/88)

In article <11656@brl-adm.ARPA> Mensing.HENR801c@Xerox.COM writes:
|>
|>Can anyone explain to me how to get my prompt to be the current directory? I
|>have tried several methods without any success. It seems that no matter what
|>method I try, the prompt is evaluated once and never changes.
|>
|>Just in case it matters, I am on an AT&T 3B2/400 running korn shell under UNIX
|>System V.3.1.
|>

This KORN SHELL SCRIPT was in the February, 1988 issue of UNIX WORLD magazine
(page 109 - Wizards Grabbag)

NOTE:  Aspen Technologies is the only company selling the TRUE Korn shell
       commerically.

       I have no relation to Aspen Technologies or UNIX World Magazine.

-- cut here for ksh script -- -- cut here for ksh script --

# @(#) Put the current directory in your shell prompt
# Author: F.C. Arndt, Aspen Technologies, Inc.
# '_cd' and 'cd' are set up to provide
# a PS1 prompt with the current directory.
# $PS0 can be set to have the user specific portion

function _cd {
	typeset t1		# define a local variable
	'cd' $1			# now change directory
# Reduce the path length to 15 chars:
	if [ ${#PWD} -gt 15 ]
	then
		t1="${PWD%/*???????????????}"
		PS1="$PS0...${PWD#$t1/}> "
	else
		PS1="$PS0$PWD> "
	fi
}
#
alias -x cd=_cd
PS1="$PS0$PWD> "
#
typeset -fx _cd

-- 
============================ US MAIL:   Lenny Tropiano, ICUS Computer Group
 IIIII   CCC   U   U   SSSS             PO Box 1
   I    C   C  U   U  S                 Islip Terrace, New York  11752
   I    C      U   U   SSS   PHONE:     (516) 968-8576 [H] (516) 582-5525 [W] 
   I    C   C  U   U      S  AT&T MAIL: ...attmail!icus!lenny  TELEX: 154232428
 IIIII   CCC    UUU   SSSS   UUCP:
============================    ...{uunet!godfre, harvard!talcott}!\
                   ...{ihnp4, boulder, mtune, bc-cis, ptsfa, sbcs}! >icus!lenny 
"Usenet the final frontier"        ...{cmcl2!phri, hoptoad}!dasys1!/

gp@picuxa.UUCP (Greg Pasquariello X1190) (02/08/88)

In article <11656@brl-adm.ARPA>, Mensing.HENR801c@Xerox.COM writes:
> 
> Can anyone explain to me how to get my prompt to be the current directory? I
> have tried several methods without any success. It seems that no matter what
> method I try, the prompt is evaluated once and never changes.
> 
> Just in case it matters, I am on an AT&T 3B2/400 running korn shell under UNIX
> System V.3.1.


Aaargh not again!  You can't believe how this same quesstion cluttered up the
netwaves recently.


Since you're running korn-shell, your in luck.  The way I have done it as 
follows:

	Create a file called .env or something like that in your home
	directory.

	Add the line PS="`pwd` > "    The '>' is not necessary, but I use
	it to delimit my prompt.		

	Add the line ENV=$HOME/.env to your .profile.

This works for me.  You should probably prepare yourself to receive a flurry
of responses to this question.

Good Luck

Greg Pasquariello
ihnp4!picuxa!gp

decot@hpisod2.HP.COM (Dave Decot) (02/09/88)

> This KORN SHELL SCRIPT was in the February, 1988 issue of UNIX WORLD magazine
> (page 109 - Wizards Grabbag)

Thanks for posting it.

> NOTE:  Aspen Technologies is the only company selling the TRUE Korn shell
>        commerically.

What distinguishes "the TRUE Korn shell" from some other Korn shell?

Dave Decot
hpda!decot

gwyn@brl-smoke.ARPA (Doug Gwyn ) (02/09/88)

In article <10650018@hpisod2.HP.COM> decot@hpisod2.HP.COM (Dave Decot) writes:
>> NOTE:  Aspen Technologies is the only company selling the TRUE Korn shell
>>        commerically.
>What distinguishes "the TRUE Korn shell" from some other Korn shell?

It's also a lie -- AT&T markets the Korn shell, through the AT&T UNIX
System ToolChest and now, I am told, bundled with some other stuff that
looks like an operating system.

davidsen@steinmetz.steinmetz.UUCP (William E. Davidsen Jr) (02/10/88)

In article <11656@brl-adm.ARPA> Mensing.HENR801c@Xerox.COM writes:
| 
| Can anyone explain to me how to get my prompt to be the current directory? I
| have tried several methods without any success. It seems that no matter what
| method I try, the prompt is evaluated once and never changes.

You use the value of PWD, which is the name you want. i.e.:
	PS1='$PWD > '
I have seen people saying that you can use `pwd` in the prompt, but
that doesn't work in any of the versions I have seen over the last few
years. $PWD does.
-- 
	bill davidsen		(wedu@ge-crd.arpa)
  {uunet | philabs | seismo}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me

ruiu@tic.UUCP (Dragos Ruiu) (02/10/88)

In article <11656@brl-adm.ARPA>, Mensing.HENR801c@Xerox.COM writes:
> Can anyone explain to me how to get my prompt to be the current directory? 
> 	Dave Mensing
> 	mensing.henr801c@xerox.com

Uh oh... please, someone email him the responses of the recent,
"Yes but how do you really get a pwd prompt ?" argue-fest.

Sorry Dave, no offence and good luck, but this was recently hashed out ad
tedium. (I'm still upset that $cwd doesn't work in uPort's csh.) I'll email
what I can remember.

Thanks,
-- 
Dragos Ruiu           UUCP:<backbone>!alberta!edson!tic!dragos!work
     Hey, remember the one who pretended to be a super-villain to get beaten ?
     Whatever happened to him ?
     He pulled it on Rorshach and Rorshach dropped him down an elevator shaft.

geoff@ncr-sd.SanDiego.NCR.COM (Geoffrey R. Walton) (02/13/88)

Bill Davidsen writes:

	that someone else wrote:

Can anyone explain to me how to get my prompt to be the
current directory? I have tried several methods without
any success. It seems that no matter what method I try,
the prompt is evaluated once and never changes.

	[so he said]

>You use the value of PWD, which is the name you want. i.e.:
>	PS1='$PWD > '
>I have seen people saying that you can use `pwd` in the prompt, but
>that doesn't work in any of the versions I have seen over the last
>few years. $PWD does.

All this is well and good for the Bourne shell, but much more is
possible with csh or (I've been told, we don't have it) ksh.

I have aliases which include history number, machine name, AND
current directory as pieces of my prompt.  Sure, it's
cumbersome, but it keeps me from getting lost as I bounce from
machine to machine (is it seven or eight?) and wander all over
the file system of each.  A long prompt is MUCH better than a
trail of bread crumbs.

All seriousness aside, here are tha aliases I use -- and no,
they DON'T add a lot of overhead; "pwd" and "uuname" are _NOT_
evaluated evertime I get a prompt, only when I change directory.

   set prompt="\!:`uuname -l`:[`pwd`] "
   alias pushd	'pushd \!* ; set prompt="\\!:`uuname -l`:[`pwd`] "'
   alias popd	'popd ; set prompt="\\!:`uuname -l`:[`pwd`] "'
   alias cd	'cd \!* ; set prompt="\\!:`uuname -l`:[`pwd`] "'

Of course, you COULD use $cwd, if you wanted.

#include <all_usual_disclaimers.h>

Geoff Walton
Software Publications
NCR E&M San Diego
geoff.walton@SanDiego.NCR.COM
or
{sdcsvax,cbosgd,pyramid,nosc.ARPA,ihnp4}!ncr-sd!geoff
Even the smallest problem becomes unsolvable if enough
meetings are held to discuss it.

wcs@ho95e.ATT.COM (Bill.Stewart) (02/14/88)

:In article <11656@brl-adm.ARPA> Mensing.HENR801c@Xerox.COM writes:
:|>Can anyone explain to me how to get my prompt to be the current directory? I
:|>have tried several methods without any success. It seems that no matter what
:|>method I try, the prompt is evaluated once and never changes.
	Korn Shell keeps the current directory in a variable $PWD,
	which is updated every time you change directory.
	In the current and current-1 versions of ksh, if the PS1
	variable contains a shell variable, it will be evaluated every
	time PS1 is displayed as a prompt.  The trick is to set
		PS1='$PWD '	## note single quotes
	so PS1 contains the string $PWD, not the value $PWD had when
	you set it (which you get with PS1="$PWD ".)  You can also use
	this with other shell variables, such as $? and $SECONDS.
-- 
#				Thanks;
# Bill Stewart, AT&T Bell Labs 2G218, Holmdel NJ 1-201-949-0705 ihnp4!ho95c!wcs

jbh@mibte.UUCP (James Harvey) (02/17/88)

In article <172@tic.UUCP>, ruiu@tic.UUCP writes:
> In article <11656@brl-adm.ARPA>, Mensing.HENR801c@Xerox.COM writes:
>> Can anyone explain to me how to get my prompt to be the current directory? 
>> 	Dave Mensing
>> 	mensing.henr801c@xerox.com
> 
> Uh oh... please, someone email him the responses of the recent,
> "Yes but how do you really get a pwd prompt ?" argue-fest.
> 
> Dragos Ruiu           UUCP:<backbone>!alberta!edson!tic!dragos!work

If such a compilation exists, I would appreciate getting a copy.
I am running under plain sys V.  The Unix World script won't
work.


-- 

Jim Harvey                        |      "Ask not for whom the bell
Michigan Bell Telephone           |      tolls and you will only pay
29777 Telegraph                   |      Station-to-Station rates."
Southfield, Mich. 48034           | 

   ihnp4!mibte!jbh   or try   ulysses!gamma!mibte!jbh
     

strong@tc.fluke.COM (Norm Strong) (02/17/88)

In article <1981@ho95e.ATT.COM> wcs@ho95e.UUCP (46323-Bill.Stewart,2G218,x0705,) writes:
>:In article <11656@brl-adm.ARPA> Mensing.HENR801c@Xerox.COM writes:
>:|>Can anyone explain to me how to get my prompt to be the current directory? I
>:|>have tried several methods without any success. It seems that no matter what
>:|>method I try, the prompt is evaluated once and never changes.
 

If you use the c-shell, you can do it the way I do:

alias cd 'chdir \!*; set prompt= "^[[7m $cwd:t ^[[0m> "; ls -aF'

Put this line in your .cshrc file and the set prompt command in your .login
file.  Every time you change directories the prompt will follow.  It is not
foolproof since it will not work if you use the chdir command to change
directories.

Incidentally the garbage around the $cwd:t is to direct my VT-100 to use
reverse video and can be eliminated if a simple prompt is adequate.


-- 

Norm   (strong@tc.fluke.com)

macbeth@artecon.UUCP (Beckwith) (02/17/88)

In article <1981@ho95e.ATT.COM> wcs@ho95e.UUCP (46323-Bill.Stewart,2G218,x0705,) writes:
<:In article <11656@brl-adm.ARPA> Mensing.HENR801c@Xerox.COM writes:
<:|>Can anyone explain to me how to get my prompt to be the current directory? I
<:|>have tried several methods without any success. It seems that no matter what
<:|>method I try, the prompt is evaluated once and never changes.
<	Korn Shell keeps the current directory in a variable $PWD,
<	which is updated every time you change directory.

I realize that this discussion ran its course many moons ago, but I did not
catch any of it. Could some kind soul send me their archive of it? We don't
use Korn Shell here, so I'm interested only in the less straightforward methods
for sh and csh.

Thanks.

-- 
+ David Macy-Beckwith  Artecon, Inc. {sdcsvax,hplabs}!hp-sdd!artecon!macbeth  +
+ The Company has enough on its plate    ||   "I didn't come here to argue!"  +
+ without supporting the crazed postings ||   "Yes, you did!"                 +
+ of its newsaholic minions.             ||   "No, I didn't!"                 +

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

   I missed the discussion on this topic, but I had a friend who inquired
about this.  I did work out a solution for csh using the change directory
command.  I put the following in my .cshrc file:

alias cd 'cd \!* ; set prompt="`pwd`> "'

so that every time I change directories the prompt will be updated.
Realize, this is slow, but it pleased a friend of mine who uses PCs every
day.

  ______
    /        /
 --/ __.  __/
(_/ (_/|_(_/_     Tad Winters
winterss%psu-cs.cs.pdx.edu            or            winters%jacobs.cs.orst.edu

bd@hpsemc.HP.COM (bob desinger) (03/02/88)

Geoffrey R. Walton (geoff@ncr-sd.SanDiego.NCR.COM) writes:
>    set prompt="\!:`uuname -l`:[`pwd`] "
>    alias pushd	'pushd \!* ; set prompt="\\!:`uuname -l`:[`pwd`] "'
>    alias popd	'popd ; set prompt="\\!:`uuname -l`:[`pwd`] "'
>    alias cd	'cd \!* ; set prompt="\\!:`uuname -l`:[`pwd`] "'

To wring every drop of speed out of it, set an envariable called, say,
$host to the value of `uuname -l` in your .login or .cshrc file.  Then
use $host in your $prompt.  (This won't work if your super-user
changes your hostname every few minutes.)

To answer the original question about doing similar things in ksh, I
can send default .profile and .kshrc files that do exactly that.  My
prompt contains the basename of the current directory and a reminder
of the host I'm on.  The actual basename program was too slow, so I
use a ksh built-in to do it.  The real work is done in one line:

	PS1="${host}_${PWD##*/}/ "

This makes my prompts look like (when I'm in, say, /usr/spool/uucp):

	a_uucp/

bob desinger	bd%hpsemc@hplabs.HP.COM		uunet!hpda!hpsemc!bd