It depends. In zsh
you can configure cd
to push the old directory on the directory stack automatically, but it is not the default setting.
As far as I can tell zsh
with default settings behaves very similar to bash
:
cd somedir
- change directory to
somedir
- save the original directory in
OLDPWD
- set
PWD="somedir"
- replace top element of the directory stack (as shown by
dirs
) with somedir
(the number of elements on the stack does not change).
cd -
:
- change directory to
$OLDPWD
- swap values of
PWD
and OLDPWD
- modify the top element of the directory stack to reflect (the new)
PWD
pushd somedir
:
- change directory to
somedir
- save original directory in
OLDPWD
- set
PWD="somedir"
- push
somedir
onto the directory stack (extending it by one element)
popd
:
- save original directory in
OLDPWD
- remove first element of the directory stack
- change directory to the new top element of the directory stack
- set
PWD
to the new top element of the directory stack
Note: Whether the present working directory is considered an element of the directory stack differs between zsh
and bash
. I used bash
as reference for the above lists.
In bash
the present working directory is considered to be the top element of the directory stack. The man 1 bash
says:
pushd [-n] [dir]
[…] Adds dir
to the directory stack at the top, making it the new current working directory as if it had been supplied as the argument to the cd
builtin. […]
Printing DIRSTACK
(echo ${dirstack[@]}
) confirms that the first element is identical to $PWD
.
In zsh
the present working directory is not part of the directory stack (but still shown with dirs
). man 1 zshbuiltins
says:
pushd [ -qsLP ] [ arg ]
[…] Change the current directory, and push the old current directory onto the directory stack. In the first form, change the current directory to arg. […]
Printing dirstack
(echo ${dirstack[@]}
) and comparing it to the output of dirs
should show that the PWD
is not part of `dirstack.
In both shells dirs
prints the present working directory as the first element. Also in both shells, the directory stack element with the index 1
refers to the directory which was current before the last pushd
. That is because arrays in zsh
are usually numbered from 1
, while they are numbered from 0
in bash
. So there is little practical difference
As said above, this behavior can be modified in zsh
.
If you set the AUTO_PUSHD
option in zsh
(setopt autopushd
) cd somedir
behaves like pushd somedir
, the previous directory is pushed onto the directory stack automatically. This is probably the case on your machine. You can run setopt
to get a list of options that are not set the default way. See, whether autopushd
appears in the list.
But this does not modify cd -
to behave like popd
. Instead it just pushes $PWD
onto the directory stack, and changes directory to $OLDPWD
. That means repeatedly calling cd -
will actually grow the directory stack (($PWD $OLDPWD $PWD $OLDPWD $PWD …)
). If it actually does behave exactly like popd
on your system, I would suggest if cd
is actually the builtin (whence -v cd
); it is possible that its replaced with an alias or function.
As the directory stack will grow rather quickly with AUTO_PUSHD
enabled, you can limit its size by setting the parameter DIRSTACKSIZE
to the desired maximum size.
You can also prevent duplicates by setting the PUSHD_IGNORE_DUPS
option.
For more options have a look at the manual.
cd
sets the shell variableOLDPWD
to the previous directory, andcd -
gets translated tocd "$OLDPWD"
.pushd
also setsOLDPWD
dirs
always holds the PWD as the first element