0

It seems that pushd and popd are not working as expected (popping the last pushed directory) but it is popping the first directory and the after the second popd it goes to home directory.

user@ubuntu:~$ pushd /tmp/dir1
/tmp/dir1 ~
user@ubuntu:/tmp/dir1$ pushd /tmp/dir2
/tmp/dir2 /tmp/dir1 ~
user@ubuntu:/tmp/dir2$ cd ..
user@ubuntu:/tmp$ popd
/tmp/dir1 ~
user@ubuntu:/tmp/dir1$ popd
~

Any reason for this?

Using Ubuntu 16.04 LTS in VMware Workstation 12 Player

1 Answer 1

0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .