UNIX / X11 users could always easily open a new xterm in the same directory as an existing xterm by typing xterm at the prompt. OS X's Terminal.app functions differently, making such a shortcut difficult. With some bash scripting, however, you can have Terminal open up each new window in the last directory you had a prompt in among your other open Terminal windows.
The trick is to add some shell code in your prompt string itself. This allows recording the current directory to a file whenever you get a new shell prompt. Then bash's default login and logout scripts take up the rest of the slack. To implement this hint, you need to edit three files: .bash_profile, .bashrc and .bash_logout.
In .bash_profile, add the following:
# nuke stale .last_cwd files.
cd
for I in .last_cwd_* ; do
[ -r "$I" ] && ( kill -0 ${I#.last_cwd_} || rm $I ) 2> /dev/null
done
# find youngest viable last_cwd and change there
L="`ls -1tr .last_cwd_* 2> /dev/null | tail -1`"
[ ! -z "$L" -a -r "$L" ] && cd "`cat $L`"
# set up PS1
export PS1='h:w u$ `echo w > ~/.last_cwd_$$`'
In .bashrc, add the following:
# set up PS1
export PS1='h:w u$ `echo w > ~/.last_cwd_$$`'
And in .bash_logout, put the following:
[ -r ~/.last_cwd_$$ ] && rm ~/.last_cwd_$$
That's it! Now, if you use a different prompt string than the default, that should still work with this hint. Just add `echo w > ~/.last_cwd_$$` to the prompt string. Also make sure the prompt string itself is specified between single quotes, so that bash defers evaluating the echo statement until it actually prints the prompt string.
Mac OS X Hints
http://hints.macworld.com/article.php?story=20051231110014263