Path: utzoo!mnetor!tmsoft!torsqnt!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!cs.utexas.edu!yale!hsdndev!cmcl2!adm!smoke!gwyn From: gwyn@smoke.brl.mil (Doug Gwyn) Newsgroups: comp.unix.questions Subject: Re: cd function Message-ID: <15235@smoke.brl.mil> Date: 17 Feb 91 20:48:49 GMT References: <2PyFX1w163w@wvus.wciu.edu> Organization: U.S. Army Ballistic Research Laboratory, APG, MD. Lines: 85 In article <2PyFX1w163w@wvus.wciu.edu> pete@wvus.wciu.edu (Pete Gregory) writes: >Hi, netters. Here's a good one for you... Yes, this is one of the better recent questions. >What I've tried: putting a function called 'cd' in .profile: >cd() >{ > cd $* > PS1="`pwd`" >} >...but alas this doesn't work because the shell uses its own internal cd >instead of running my function. This is properly viewed as a deficiency in the support provided by the Bourne shell, which is why 8th Edition UNIX added the "builtin" shell builtin. BRL (mostly Doug Kingston) implemented "builtin" in the version of the Bourne shell that I maintain and make available upon request to properly-licensed sites. (UNIX System V Release 2.0 or later source license required.) Our shell has many independently- implemented features similar to those found in other shells such as the Korn shell, although different in detail. (We like ours better, and were inspired by the 8th Edition UNIX shell features.) Here is how my startup "dot files" redefine the "cd" command; doing what you were attempting is trivial, but mine also takes care of supporting a form of "working directory history" and providing host and current working directory information in either the $PS1 prompt or in a window banner (title), depending on the type of terminal being used. It will undoubtedly grow larger when I add support for X-Windows. (I'm considering introducing a separate function for banner manipulation.) While you couldn't use this example directly, out of the context that I establish for my shell working environment, it does show that the basic idea is workable, if your shell provides the proper support. Until and unless you find a better shell, you might consider naming your function something like "cwd" and getting into the habit of using that instead of typing "cd". # If the "builtin" command (BRL shell) doesn't exist, the following # will not be invoked (always get built-in "cd"), so its use of # "builtin" is not a problem. cd(){ PREVDIR="$CWD" if [ $# -lt 1 ] then builtin cd else builtin cd "$1" fi CWD=`pwd` export CWD # exported so interactive subshells can cd $CWD to outwit symbolic links set +u if [ "$CWD" = "$HOME" ] then PFX="$HOST" PS1= else PFX="$HOST": PS1=`echo "$CWD" | sed -e "s!^$HOME!~!" -e "s!^/usr/src/s5!~sys5!"` fi if [ "$TERM" = att630 -o "$TERM" = tty630 ] then if [ -z "$PS1" ] then PS1=:~ fi echo '\033[?'`expr "$PFX$PS1" : '.*'`";0v$PFX$PS1\c" PFX= PS1= elif [ "$MYXBAN" ] then case "$TERM" in sun) echo "\033]L $HOST\033\\\\\c" echo "\033]l $PFX$PS1\033\\\\\c" PFX= PS1= ;; *5620*) # "myx" assumed; ~/.myxban -> $DMD/local/bin/myxban if [ -z "$PS1" ] then PS1='~' fi if [ -x ~/.myxban ] then ~/.myxban -l " $HOST" ~/.myxban -r "$HOST " ~/.myxban -c "$PS1" fi PFX= PS1= ;; esac fi PS1="$PFX$PS1"'$ ' unset PFX set -u }