Bourne Shell Question?

Steve Hayman sahayman at iuvax.cs.indiana.edu
Thu Feb 2 15:00:15 AEST 1989


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 at iuvax.cs.indiana.edu



More information about the Comp.unix.questions mailing list