[comp.unix.questions] Bourne Shell Question?

dap9702@dasys1.UUCP (Dan Powers) (02/01/89)

Hi All,
  Does anyone know how to get a prompt in Bourne Shell that contains the
current working directory (pwd). I can get this to work in C shell by 
specifing an alias for cd as 'cd \!*;set prompt="[`pwd`]% "'. Any and all
help would be appreciated.
DP

-- 
Dan Powers
Big Electric Cat Public UNIX
..!cmcl2!phri!dasys1!dap9702

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

dap9702@dasys1.UUCP (Dan Powers) writes:
\  Does anyone know how to get a prompt in Bourne Shell that contains the
\current working directory (pwd).

One of the most frequently asked questions; it IS possible, but there is a
price you have to pay.

Solution I.
------------------------------cut here------------------------------
#! /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
------------------------------cut here------------------------------

Solution II.
If your sh knows shell functions:
------------------------------cut here------------------------------
c()		# you cannot name this function `cd'
{
	cd $1
	PS1="`pwd`> "
}
-- 
 "Does she play, er, tennis?          |Maarten Litmaath @ VU Amsterdam:
             Wink wink, notch notch!" |maart@cs.vu.nl, mcvax!botter!maart

tkopp@carroll1.UUCP (Tom Kopp) (02/02/89)

In article <8517@dasys1.UUCP>, dap9702@dasys1.UUCP (Dan Powers) writes:
>   Does anyone know how to get a prompt in Bourne Shell that contains the
> current working directory (pwd). I can get this to work in C shell by 
> specifing an alias for cd as 'cd \!*;set prompt="[`pwd`]% "'. Any and all
> help would be appreciated.
> DP

I have not yet seen this done on a Bourne Shell, and I have asked a few
people and none of them know how to do it.  I seem to remember an easier
way to do it in csh, but I cannot remember (has been 6 mos + since I've 
seen the method).  Some particular reason you need it in Bourne shell only?
The only shell I have figured out how to do it in is Korn shell.  As an 
example, using

PS1='$PWD >'

gives me

/assist/tkopp >

when in home directory.


-- 
"Those who do not understand Unix      |Thomas J. Kopp
are condemned to re-invent it, Poorly" |tkopp@carroll1.UUCP
- Henry Spencer                        |uunet!marque!carroll1!tkopp

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

In article <8517@dasys1.UUCP> dap9702@dasys1.UUCP (Dan Powers) writes:
>  Does anyone know how to get a prompt in Bourne Shell that contains the
>current working directory (pwd).

Not all Bourne shells have sufficient capabilities to do this.  I think
for the Korn shell one would use "alias" in a manner similar to your
Cshell example.  On UNIX System V Release 2 and later shells (i.e. those
supporting shell functions), you can define a function (unfortunately
not called "cd"; use "ch" instead, for example) that sets the shell
variable PS1 appropriately as it does the "cd").  For 8th or 9th Edition
UNIX shells and the BRL Bourne shell, you can use the "builtin" builtin
to redefine "cd", for example:

cd(){
	if [ $# -lt 1 ]
	then	builtin cd
	else	builtin cd "$1"
	fi
	if [ `pwd` = "$HOME" ]
	then	PS1='$ '
	else	PS1=`echo "$CWD" | sed "s!^$HOME!~!"`'$ '
	fi
}

(My actual "cd" definition is MUCH more elaborate than this.)

You can adapt the above to SVR2 shells by renaming the function and
removing the "builtin" keywords.

eirik@lurch.Stanford.EDU (Eirik Fuller) (02/02/89)

In article <201@carroll1.UUCP>, tkopp@carroll1 (Tom Kopp) writes:
>In article <8517@dasys1.UUCP>, dap9702@dasys1.UUCP (Dan Powers) writes:
>>   Does anyone know how to get a prompt in Bourne Shell that contains the
>> current working directory (pwd). I can get this to work in C shell by 
> ...
>I have not yet seen this done on a Bourne Shell, and I have asked a few
>people and none of them know how to do it.
> ...

It's been too long since the last time the following answer was
posted.  If I remembered who last posted it, I'd give credit.  I
thought it was hilarious last time; certainly no worse than the
periodic asking of this perpetual question.  The answer is ...


PS1=.


Hahahahahaha

I liked this answer so much when I first saw it that ever since, every
Unix account I've got has "set prompt=." in .cshrc

sahayman@iuvax.cs.indiana.edu (Steve Hayman) (02/02/89)

Every year someone asks "How can I put the current directory in 
my bourne shell prompt".  Every year some people reply "Most
bourne shells can't do that."

How 'bout this.  (tested under ultrix 3.0 only but (famous last
words) I think it will work on most bourne shells.)

The idea is to write a script called "chd" that puts its argument
in a file and sends a signal to the parent shell; the parent
traps this signal and resets PS1 according to the contents
of the file, and cd's to the contents of the file.

In your .profile

    # Put the ID of this login shell into the environment so that
    # "chd" can get at it
    SHELLPID=$$ export SHELLPID

    # A signal that "chd" will use to cause our login shell
    # to reevaluate its prompt.  I don't know what the best
    # one to use would be but 16 (SIGURG) seemed fairly
    # obscure.  Too bad our /bin/sh won't let you trap SIGUSR1.

    PROMPTSIG=16 export PROMPTSIG

    # What to do when we get the prompt signal.
    # The prompt will be set to  "directory$ ".  You could
    # easily use some other format.
    
    trap 'PS1="`cat /tmp/cd-$SHELLPID`\$ "; cd `cat /tmp/cd-$SHELLPID`' $PROMPTSIG

In an executable file called "chd" somewhere in your PATH, put this, which
conveniently produces an error message if the named directory doesn't exist.

    cd $1 && echo $1 >/tmp/cd-$SHELLPID && kill -$PROMPTSIG $SHELLPID


And you just use "chd directory" and voila, current directory
in your prompt (except for the first time you log in):


    $ chd /tmp
    /tmp$ chd /usr
    /usr$ pwd
    /usr
    /usr$ chd no-such-dir
    chd: no-such-dir: bad directory
    /usr$ pwd
    /usr


Now, I don't use /bin/sh as my login shell, and I only tried this
for about five minutes, so someone on the net
may well find some reason why this won't work in general.  But
it might be a start.

..Steve
-- 
Steve Hayman    Workstation Manager    Computer Science Department   Indiana U.
sahayman@iuvax.cs.indiana.edu

leo@philmds.UUCP (Leo de Wit) (02/02/89)

In article <8517@dasys1.UUCP> dap9702@dasys1.UUCP (Dan Powers) writes:
|Hi All,
|  Does anyone know how to get a prompt in Bourne Shell that contains the
|current working directory (pwd).

If you can cope with a somewhat wierd syntax, you could do it this way:

[/usr/dan] $c bin $d
[/usr/dan/bin]

The c and d are environment variables that are set up this way:

c='eval cd'
d='; PS1="[`pwd`] "'
export c d PS1

You can stick this in your .profile or in a file you . from within .profile.

      Leo.

ekrell@hector.UUCP (Eduardo Krell) (02/02/89)

In article <9562@smoke.BRL.MIL> gwyn@brl.arpa (Doug Gwyn <gwyn>) writes:

>I think
>for the Korn shell one would use "alias" in a manner similar to your
>Cshell example.

You don't need an alias. All you need is to put $PWD in your PS1
prompt. I like PS1='(${PWD#$HOME/}) ' which displays your working
directory relative to your home directory. This is from the ksh book.
    
Eduardo Krell                   AT&T Bell Laboratories, Murray Hill, NJ

UUCP: {att,decvax,ucbvax}!ulysses!ekrell  Internet: ekrell@ulysses.att.com

mchinni@ardec.arpa (Michael J. Chinni, SMCAR-CCS-E) (02/02/89)

Dan,

	Here is another idea. It is known to work in the BSD 4.3 environment.
It MUST be put in your .profile.  Once it has been setup (in .profile and
.profile executed as if at login time), use "c" instead of "cd".

c()
{
 case $# in
    1) cd $1;;
    *) cd $HOME;;
 esac
 PS1=`pwd`"> ";
}
c

Example if `pwd` is /usr/sys, prompt will be "/usr/sys> ".

/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
			    Michael J. Chinni
	US Army Armament Research, Development, and Engineering Center
 User to skeleton sitting at cobweb    ()   Picatinny Arsenal, New Jersey  
   and dust covered terminal and desk  ()   ARPA: mchinni@ardec.arpa
    "System been down long?"           ()   UUCP: ...!uunet!ardec.arpa!mchinni
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

davidsen@steinmetz.ge.com (William E. Davidsen Jr) (02/03/89)

In recent versions of sh you can use a function with a similar name to
do what you want.
	cwd() { cd $1 && PS1="`pwd`> "; }

In ksh you can use the $PWD, as:
	PS1='$PWD> '
or (my favorite) strip the HOME from the display with:
	PS1='${PWD#$HOME/}> '
This 2nd form strips the HOME part of the directory when you are in a
subdirectory of your HOME directory, and leaves a prompt starting with a
driectory name. In all other directories, including HOME, you get the
absolute path name, starting with a /.

Hope this isn't more info than you wanted.
-- 
	bill davidsen		(wedu@ge-crd.arpa)
  {uunet | philabs}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me