Kevin's answer is excellent. I've written up some details about what's going on, in case people are looking for a better understanding of why their script is necessary to solve the problem.
The reason that pushd . breaks the behavior of cd - will be apparent if we dig into the workings of cd and the directory stack. Let's push a few directories onto the stack:
$ mkdir dir1 dir2 dir3 $ pushd dir1 ~/dir1 ~ $ pushd../dir2 ~/dir2 ~/dir1 ~ $ pushd../dir3 ~/dir3 ~/dir2 ~/dir1 ~ $ dirs -v 0 ~/dir3 1 ~/dir2 2 ~/dir1 3 ~
Now we can try cd - to jump back a directory:
$ cd - /home/username/dir2 $ dirs -v 0 ~/dir2 1 ~/dir2 2 ~/dir1 3 ~
We can see that cd - jumped us back to the previous directory, replacing stack ~0 with the directory we jumped into. We can jump back with cd - again:
$ cd - /home/username/dir3 $ dirs -v 0 ~/dir3 1 ~/dir2 2 ~/dir1 3 ~
Notice that we jumped back to our previous directory, even though the previous directory wasn't actually listed in the directory stack. This is because cd uses the environment variable $OLDPWD to keep track of the previous directory:
$ echo $OLDPWD /home/username/dir2
If we do pushd . we will push an extra copy of the current directory onto the stack:
$ pushd . ~/dir3 ~/dir3 ~/dir2 ~/dir1 ~ $ dirs -v 0 ~/dir3 1 ~/dir3 2 ~/dir2 3 ~/dir1 4 ~
In addition to making an extra copy of the current directory in the stack, pushd . has updated $OLDPWD:
$echo $OLDPWD /home/username/dir3
So cd - has lost its useful history, and will now just move you to the current directory - accomplishing nothing.
pwdto figure out where you are?cd -that I want (or expect). I know perfectly well in which directory I am, but I want to increase the speed with which I change directories :)zsh? It has really nice features like AUTO_PUSHD.