It goes to home directory after last pop because you were in your home directory when you pushed /tmp/dir1
onto this stack. ~
is your home directory. This part:
user@ubuntu:~$ pushd /tmp/dir1
/tmp/dir1 ~
Specifically, when the directory stack is empty, it adds current working directory to the bottom of the stack.
Here's an example from /etc
:
bash-4.3$ cd /etc
bash-4.3$ pushd /usr
/usr /etc
The popd
behavior is also proper, it did remove the oldest pushed directory, but you also missed the part that it goes to the new top directory after the old was removed. Read the manual again:
popd [-n] [+n] [-n] Removes entries from the directory stack.
With no arguments, removes the top directory from the stack, and performs a cd to the new top directory.
So in your case with stack /tmp/dir2 /tmp/dir1 ~
, first popd
removed /tmp/dir2
. So what's the new top of the stack ? /tmp/dir1
, so it will also perform cd /tmp/dir1
.
Observe:
bash-4.3$ pwd
/etc
bash-4.3$ pushd /bin
/bin /etc
bash-4.3$ pushd /usr
/usr /bin /etc
bash-4.3$ pushd /sys
/sys /usr /bin /etc
bash-4.3$ popd
/usr /bin /etc
bash-4.3$ pwd
/usr