0

If I poorly write a script that uses pushd /etc but I don't finish it with popd: Will /etc still be in the pushd+popd directory stack/in RAM after the bash script has finished executing and Bash has terminated?

Also does the pushd+popd directory stack clear (like variables do) if I close the terminal emulator session (which should terminate the bash shell process for that terminal emulator session) without manually clearing it with popd?

3

The directory stack that the pushd and popd commands in bash use is local to the current shell. This means that if you don't use popd in a script, this will not influence the directory stack outside of the script.

The stack will be emptied if you close the terminal (since the shell terminates).

The directory stack is also empty by default whenever you start a new bash shell.

1

There are some easy experiments you can run to find the answers to your questions. Sometimes the best way to learn learn new tools is to play around with them.

If I poorly write a script that uses pushd /etc but I don't finish it with popd: Will /etc still be in the pushd+popd directory stack/in RAM after the Bash script has finished executing and Bash has terminated?

$ cd /tmp
$ echo 'pushd /etc' > experiment.sh
$ bash experiment.sh
/etc /tmp
$ pwd
/tmp
$

Above, experiment.sh is the poorly-written script. It uses pushd to push the current directory on the stack and switch to /etc/ then terminates. After the script terminated, the running shell was still in /tmp.

Also does the pushd+popd directory stack clear (like variables do) if I close the terminal emulator session (which should terminate the Bash shell process for that terminal emulator session) without manually clearing it with popd?

$ cd /tmp
$ pushd /etc
/etc /tmp
$ dirs
/etc /tmp
-- close terminal --
-- open new terminal --
$ dirs
~

Above in one shell we use pushd to push the current directory on the directory stack, close the terminal, then open a new terminal. In the new terminal we see that the directory that was previously pushed is not on the stack.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.