You involve several different questions in one. :P
We'll cover variable assignment first.
To put the result of a command into a variable you can either use backticks `` or $() syntax. For example:
For further information check the section Command Substitution in the bash(1) manual page. Easily available at: http://linux.die.net/man/1/bash
But if for your question is about changing to the previous directory bash has a built in syntax for this. Simply do a
cd - and bash will take you to where you were.
# cd /usr/local/bin
# cd /var/log/apache2
# cd -
For more advanced directory/cd handling, check the manual page for
popd that allows you to build a stack of directories that you can use.