1
$ pwd
/tmp/d1
$ dirs
/tmp/d1
$ pushd ../d2
/tmp/d2 /tmp/d1
$ popd
/tmp/d1
$ pwd
/tmp/d1

It seems that pushd always pushes the current directory e.g. /tmp/d1 into the stack, although I run it as pushd ../d2?

Note that: http://linux.101hacks.com/cd-command/dirs-pushd-popd/

The first directory of the dir command output is always the current directory and not the content from the stack.

Tim
  • 101,790

2 Answers2

2

No pushd does not always push the current directory. From bash' man page:

   pushd [-n] [+n] [-n]
   pushd [-n] [dir]
          Adds  a directory to the top of the directory stack, or rotates
          the stack, making the new top of the stack the current  working
          directory.   With  no arguments, exchanges the top two directo‐
          ries and returns 0, unless the directory stack is empty.
          .
          .
          dir    Adds  dir  to  the directory stack at the top, making it
                 the new current working directory.

The form with a directory pushd [dir] is not using the currently directory unless that directory is explicitly used as the argument. Doing pushd somedir

Anthon
  • 79,293
1

I am using zsh and it has an option PUSHD_IGNORE_DUPS: "Don’t push multiple copies of the same directory onto the directory stack.". You might need to check your shell options (for zsh I needed to unsetopt pushdignoredups.)

Carl G
  • 111