一、
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
) withsomedir
(the number of elements on the stack does not change).
- change directory to
-
cd -
:- change directory to
$OLDPWD
- swap values of
PWD
andOLDPWD
- modify the top element of the directory stack to reflect (the new)
PWD
- change directory to
-
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)
- change directory to
-
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
- save original directory in
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. Theman 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 thecd
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 withdirs
).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 ofdirs
should show that thePWD
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.
参考:https://unix.stackexchange.com/questions/272965/pushd-popd-vs-cd-cd-in-bash-and-zsh