[comp.unix.wizards] 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

ron@topaz.rutgers.edu (Ron Natalie) (02/09/88)

This works with any real shell:

    PS1="."

(I don't recall who posted this, but it was one of the best answers I
heard on it).

On the Korn shell (but not most Bourne shell PS1 is evaluated each time
it is printed so you can:

    PS1="`pwd` "

-Ron

kevinc%bearcat.lim.tek.com@relay.cs.net (Kevin Cosgrove 627-5212) (02/09/88)

Dave,

> 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

	I use the following alias with  C shell.  I put the following line
	in my ~/.cshrc file.

	alias  cd 'chdir \!*;set prompt="[`whoami`@`hostname`-$cwd:t] "'
_____________________________________________________________________________

	Kevin Cosgrove           	Tektronix, Inc.
	11K Plug-Ins Project Leader	PO Box 500, M/S 47-092
	LIM Product Test Engineering 	Beaverton, OR  97077
	kevinc@bearcat.LIM.TEK.COM	(503)-627-5212
_____________________________________________________________________________

dik@cwi.nl (Dik T. Winter) (02/09/88)

(Again all this prompt stuff?)
In article <11724@brl-adm.ARPA> kevinc%bearcat.lim.tek.com@relay.cs.net (Kevin Cosgrove 627-5212) writes:
 > > ...
 > > Just in case it matters, I am on an AT&T 3B2/400 running korn shell under UNIX
 > > ...
 > 
 > 	I use the following alias with  C shell.  I put the following line
Pretty helpful.
-- 
dik t. winter, cwi, amsterdam, nederland
INTERNET   : dik@cwi.nl
BITNET/EARN: dik@mcvax

cjc@ulysses.homer.nj.att.com (Chris Calabrese[rs]) (02/09/88)

In article <17877@topaz.rutgers.edu>, ron@topaz.rutgers.edu.UUCP writes:
> [...]
> 
> On the Korn shell (but not most Bourne shell PS1 is evaluated each time
> it is printed so you can:
> 
>     PS1="`pwd` "
> 
> -Ron


The Korn shell also has an internal variable which keeps track of
the current directory, thus
	PS1=$PWD
works just as well, only a zillion times faster.

	Chris Calabrese
	AT&T Bell Labs
	ulysses!cjc

rjd@occrsh.ATT.COM (02/10/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

 Well, since we just went through this in one of the groups I read (this one?),
and I am also running ksh for my login, this was the method that I liked the
best.  Put into your .profile (or the file that ENV is set to):

c() {
	cd $*
	PS1="${PWD}: "
}

  And use "c <dirname>" to change directories.

Randy

guy@gorodish.Sun.COM (Guy Harris) (02/10/88)

> Summary: don't have to run the pwd program

Who said anything about running the "pwd" program?

	gorodish$ type pwd
	pwd is an exported alias for print - $PWD
	gorodish$ type print
	print is a built-in command

(Version 02/21/85c) I'd be *very* surprised if references to "$PWD" were "a
zillion times faster" than calls to the "pwd" command.
	Guy Harris
	{ihnp4, decvax, seismo, decwrl, ...}!sun!guy
	guy@sun.com

sullivan@vsi.UUCP (Michael T Sullivan) (02/10/88)

In article <17877@topaz.rutgers.edu>, ron@topaz.rutgers.edu (Ron Natalie) writes:
> This works with any real shell:
> 
>     PS1="."
...
> On the Korn shell (but not most Bourne shell PS1 is evaluated each time
> it is printed so you can:
> 
>     PS1="`pwd` "
The current (February) issue of "Unix World" has a nice little script for
this written by Aspen Technologies, Inc., the ksh people.  It's on page 109
in the Wizard's Grabbag.

Michael Sullivan
{ucbvax | ihnp4 | sun}!amdcad!uport!vsi!sullivan
{uunet | attmail}!vsi!sullivan

ron@topaz.rutgers.edu (Ron Natalie) (02/10/88)

Actually, pwd is also a builtin.  It's only half a zillion times faster
to use the variable.

-Ron

kwok@iris.ucdavis.edu (Conrad Kwok) (02/10/88)

I have a program to set my prompt to the current directory. Using the
program you can get several feature that you cannot get by using
shell script.
o the home directory is abbreviated to "~" so that the length
  of the prompt is shorter when you are working under your home
  directory.
o When the length of the path is greater than a certain number (currently
  set to 25), the upper level directory is abbreviated to "...". so you
  won't get a prompt close to your line width.

Due the problem of link (hard or soft link), the home directory
name may be different from the name get from the system call getcwd.
One way to get around this is to write any program to set an
environment variable say TRUEHOME to your home directory name get from
getcwd in .login. But the way I choose now is to hard code it in the
program. Therefore when you get the program be sure to set the string
HOME to your home directory.

This program has been used on ULTRIX 2.0, SUN-3. But on BSD 4.2 or 4.3
you may have to change getcwd to getwd according to the syntax of getwd.

Good Luck!

-- Conrad

-----------------------put them in .cshrc----------------
alias cd 'cd \!*; set prompt="`~/bin/prompt` "'
alias pushd 'pushd \!*; set prompt="`~/bin/prompt` "'
alias popd 'popd; set prompt="`~/bin/prompt` "'
-------------------------prompt.c-------------------------
#include <stdio.h>

#define MAXLEN 80
#define PATHLEN 25


char HOME[]="/YOUR/HOME/DIRECTORY";

/*
char HOME[MAXLEN];
*/
char *getenv();

main(argc,argv)
int argc;
char **argv;
{
    char cwd[MAXLEN+2], *newpath, *tmpptr;
    int loop;

/*
    strcpy(HOME,getenv("TRUEHOME"));
*/
    if (getcwd(cwd,MAXLEN)==NULL) {
	fprintf(stderr,"Error in reading working directory name\n");
	printf("%%");
	exit(1);
    }
    tmpptr=HOME;
    newpath=cwd;
    for(loop=strlen(HOME)-1; loop>=0; loop--) {
	if (*tmpptr++ != *newpath++) break;
    }
    if (loop>=0)
        newpath=cwd;
    else
	*(--newpath)='~';
    while (strlen(newpath) > PATHLEN) {
	newpath+=4;
  	while (*newpath!='/' && *newpath!=NULL) newpath++;
	for (loop=3; loop>0; loop--) *(--newpath)='.';
    }
    strcat(newpath,">");
    printf("%s", newpath);
}

-------------------
internet: kwok@iris.ucdavis.edu
csnet: kwok@ucd.csnet 
csnet: kwok%iris.ucdavis.edu@csnet.relay
uucp: {ucbvax, uunet, lll-lcc, ...}!ucdavis!iris!kwok

dave@stcns3.stc.oz (Dave Horsfall) (02/11/88)

In article <17877@topaz.rutgers.edu> ron@topaz.rutgers.edu (Ron Natalie) writes:
>
>On the Korn shell (but not most Bourne shell PS1 is evaluated each time
>it is printed so you can:
>
>    PS1="`pwd` "

It would be better to use PS1="\${PWD}".  Although "pwd" is a built-in anyway.

-- 
Dave Horsfall (VK2KFU)      ACS:  dave@stcns3.stc.OZ.AU
Alcatel-STC Australia       ARPA: dave%stcns3.stc.OZ.AU@uunet.UU.NET
11th Floor, 5 Blue St       UUCP: {enea,hplabs,mcvax,uunet,ukc}!\
North Sydney NSW 2060 AUSTRALIA    munnari!stcns3.stc.OZ.AU!dave

lm@arizona.edu (Larry McVoy) (02/11/88)

In article <1079@ucdavis.ucdavis.edu> kwok@iris.UUCP (Conrad Kwok) writes:
>
>I have a program to set my prompt to the current directory. Using the

Jeez, louise, run a program everytime you cd???  Argggg.  Here's a ksh version
that also extends ksh's "cd -" to "cd --" and "cd ---" to get the last
three dirs you were in (so my cache is small; you make it bigger).  By 
some strange coincidence it also does the ~ trick.  Hmmmm....

oh yeah, you also get $OLDPWD1 and $OLDPWD as well as the normal $OLDPWD.
Sometimes useful.

--- cut here and feed to ksh ----
function _cd {
	SAVEPWD1=$OLDPWD
	SAVEPWD2=$OLDPWD1
	case $1 in
	--) 'cd' $OLDPWD1;;
	---) 'cd' $OLDPWD2;;
	*) 'cd' $1;;
	esac
	OLDPWD2=$SAVEPWD2
	OLDPWD1=$SAVEPWD1

	case $PWD in
	$HOME*)		PS1=~"${PWD##$HOME} ";;
	*)		PS1="$PWD ";;
	esac
}

function dirs {
	print "$OLDPWD  $OLDPWD1  $OLDPWD2"
}

alias d=dirs
alias cd=_cd
alias CD='cd'
-- 
Larry McVoy	lm@arizona.edu or ...!{uwvax,sun}!arizona.edu!lm
		Use the force - read the source.

morgan@brambo.UUCP (Morgan W. Jones) (02/12/88)

From article <17877@topaz.rutgers.edu}, by ron@topaz.rutgers.edu (Ron Natalie):
} This works with any real shell:
} 
}     PS1="."
} 
} (I don't recall who posted this, but it was one of the best answers I
} heard on it).
} 
} On the Korn shell (but not most Bourne shell PS1 is evaluated each time
} it is printed so you can:
} 
}     PS1="`pwd` "
} 
} -Ron

Doesn't work on mine (SysVR3 on Prime EXL316).

-- 
Morgan Jones - Bramalea Software Inc.       ...!utgpu!telly \ !brambo!morgan
               ...!{uunet!mnetor, watmath!utai}!lsuc!ncrcan /
"These might not even be my opinions, let alone anyone else's."

gerry@syntron.UUCP (G. Roderick Singleton) (02/13/88)

In article <11724@brl-adm.ARPA> kevinc%bearcat.lim.tek.com@relay.cs.net (Kevin Cosgrove 627-5212) writes:
<Dave,
<
<> 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
<
<	I use the following alias with  C shell.  I put the following line
<	in my ~/.cshrc file.
<
<	alias  cd 'chdir \!*;set prompt="[`whoami`@`hostname`-$cwd:t] "'
<_____________________________________________________________________________
<
<	Kevin Cosgrove           	Tektronix, Inc.
<	11K Plug-Ins Project Leader	PO Box 500, M/S 47-092
<	LIM Product Test Engineering 	Beaverton, OR  97077
<	kevinc@bearcat.LIM.TEK.COM	(503)-627-5212
<_____________________________________________________________________________


For those with source lisences, there is yet another solution.  Install
the Georgia Tech modifications on /bin/sh and your requirement is met within
sh, itself.  Even better news you get tilde name and history without the
overhead of /bin/csh.  These were posted some time ago but are still available
from most of the archive sites.  Makes using Bourne shell worthwhile.
-- 
G. Roderick Singleton              |  "ALL animals are created equal,
   <gerry@syntron.uucp>,           |   BUT some animals are MORE equal
or <gerry@geac.uucp>,              |   than others." a warning from
or <gerry@eclectic.uucp>           |  "Animal Farm" by George Orwell

tim@ism780c.UUCP (Tim Smith, Knowledgian) (02/18/88)

In article <142700023@occrsh.ATT.COM> rjd@occrsh.ATT.COM writes:
< and I am also running ksh for my login, this was the method that I liked the
< best.  Put into your .profile (or the file that ENV is set to):
< 
< c() {
< 	cd $*
< 	PS1="${PWD}: "
< }
< 
<   And use "c <dirname>" to change directories.

Here is a function that works with the regular shell.  It has some obvious
drawbacks, but it sort of works:

CD=				# holds current directory
ccd() {
	SAVE=$CD
	if [ $# -eq 0 ]
	then
		CD='~'
		NEW=
	else
		NEW=$1
		case $NEW
		in
			/* ) CD=$NEW ;;
			 * ) CD=$CD/$NEW ;;
		esac
	fi
	if cd $NEW
	then
		PS1=$CD
	else
		CD=$SAVE
	fi
	PS1="$CD "
}

use "ccd <dirname>" to change directories.  This will show your home
directory as '~'.
-- 
Tim Smith					tim@ism780c.ism.com
"There is no dark side of the force.  As a matter of fact, it's all dark"

jcz@sas.UUCP (John Carl Zeigler) (02/23/88)

Actually, the differnces between 'pwd' (aka print $pwd) and
'/bin/pwd' include:

(cwd = '/net/chopin/foo/bar/blech' )

	$ /bin/pwd
	/net/chopin/foo/bar/blech
	$ pwd
	/foo/bar/blech

on HP-UX systems with RFA, that is.   Very annoying,
took me too long to find this in a shell script.  sigh

--jcz

-- 
--jcz
John Carl Zeigler
SAS Institute Inc.
Cary, NC  27511           (919) 467-8000        ...!mcnc!rti!sas!jcz