One thing you can do in a script or on the command line is to not use the cd
command to change directories, but use the pushd and popd commands to keep a
directory stack. You can even save environment variables that define directory
paths or even the current directory stack with the dirs -l command:
curdirstk=`dirs -l`
If you use cd in a script to change the directory from the current directory
from which the script is launched, after the script exits, the current directory
is the same as the one from which the script was launched - because the script
starts up a subprocess environment which has no choice in the matter but to
expire when done which cancels any directory changes and subprocess environment
thus leaving you back in the current directory from which the script was
launched.