Change directory cd to another terminal's cwd

I would like to create a command like cd - (lets call it cdp) that will change directories to the last changed-to directory from another terminal window similar to the option to open a new terminal in the directory that previous window/tab was in (I don't see that option in Mac OS X terminal).

To do so I figure I could alter cd with something like alias cd='cd $1;echo $PWD > /tmp/CWD' and then add

alias cdp='cd  `cat /tmp/CWD`

Can someone key in a better solution? Or, fill me in on an existing program, feature, etc., please let me know. On Mac OS X 10.6 with default terminal. Thanks.


Solution 1:

Aliases don't accept parameters. You'll have to use a function. You should also use the command builtin.

function cd () { command cd "$@"; echo "$PWD" > /tmp/CWD; }

alias cdp='cd "$(</tmp/CWD)"'

See this for another approach that's specific to OS X. It's a script that can launch a new Terminal window or tab opening with its current directory the same as that of the current Terminal window or tab.