push/pop current directory?
There is pushd
and popd
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.
More to read
Example:
$ pwd; pushd /tmp; pwd; popd; pwd
/home/me
/tmp ~
/tmp
~
/home/me
Calling bash
starts a new subshell, which has its own input; none of the other commands will run until it exits. Surrounding the commands to be run with parens will also start a new subshell, but it will run the commands within it.
( cd dir ; ./dostuff )
If you don't need multiple levels of directory history, you can also do:
cd foo
# do your stuff in foo
cd -
Compared to pushd
/popd
, this has the disadvantage that if cd foo
fails, you end up in the wrong directory with cd -
.
(Probably cd -
is more handy outside scripts. "Let's go back where I just was.")