popd, how to push the current directory onto the stack?
$ pwd; pushd /tmp; pwd; popd; pwd /home/users/foo /tmp ~ /tmp ~ /home/users/foo
Bash will keep a history of the directories you visit, you just have to ask. Bash stores the history in a stack and uses the commands pushd and popd to manage the stack.
If you don't need multiple levels of directory history, you can also do:
cd foo # do your stuff in foo cd -
popd, this has the disadvantage that if
cd foo fails, you end up in the wrong directory with
cd - is more handy outside scripts. "Let's go back where I just was.")
See Use pushd and popd to manipulate directory stack for more help.