Is there a difference between the behavior of pushd/popd in bash vs zsh? It seems in zsh cd, cd- behaves exactly the same as pushd/popd (which adds/pops directory automatically when cd) while in bash cd doesn’t affect the dir stack.
If someone can give me a pointer that would be great.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
PWDandOLDPWD - 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
somedironto 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
PWDto 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
bashthe present working directory is considered to be the top element of the directory stack. Theman 1 bashsays:pushd [-n] [dir]
[…] Addsdirto the directory stack at the top, making it the new current working directory as if it had been supplied as the argument to thecdbuiltin. […]Printing
DIRSTACK(echo ${dirstack[@]}) confirms that the first element is identical to$PWD. -
In
zshthe present working directory is not part of the directory stack (but still shown withdirs).man 1 zshbuiltinssays: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 ofdirsshould show that thePWDis 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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0