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.
pushd, popd vs cd, cd- in bash and zsh
bash;zsh;cd command;pushd
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 somedirchange directory to somedirsave the original directory in OLDPWDset PWD=somedirreplace 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 $OLDPWDswap values of PWD and OLDPWDmodify the top element of the directory stack to reflect (the new) PWDpushd somedir: change directory to somedirsave original directory in OLDPWDset PWD=somedirpush somedir onto the directory stack (extending it by one element)popd:save original directory in OLDPWDremove first element of the directory stackchange directory to the new top element of the directory stackset PWD to the new top element of the directory stackNote: 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 differenceAs 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.