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

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.

  • pushd -n ../d2 is what you are looking for? – jimmij Oct 11 '14 at 22:34
  • Seems yes. I dont understand how pushd works. – Tim Oct 11 '14 at 22:40

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

| improve this answer | |
  • Thanks. But I don't see pushd somedir push somedir into the stack. – Tim Oct 11 '14 at 22:10

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.)

| improve this answer | |

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.