一、

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 autopushdcd 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

003_cd pushd popd三个命令的区别的更多相关文章

  1. [转]Linux中set,env和export这三个命令的区别

    转自:http://www.2cto.com/os/201306/223758.html Linux中set,env和export这三个命令的区别   set命令显示当前shell的变量,包括当前用户 ...

  2. 命令stat anaconda-ks.cfg会显示出文件的三种时间状态(已加粗):Access、Modify、Change。这三种时间的区别将在下面的touch命令中详细详解:

    7.stat命令 stat命令用于查看文件的具体存储信息和时间等信息,格式为"stat 文件名称". stat命令可以用于查看文件的存储信息和时间等信息,命令stat anacon ...

  3. pushd,popd,dirs,cd -让切换目录更方便

    与linux cd命令相似,用pushd实现在不同目录间切换 在命令行模式下,当你工作在不同目录中,你将发现你有很多时间都浪费在重复输入上如果这些目录不在同一个根目录中,你不得不在转换时输入完整的路径 ...

  4. [转]Ubuntu中apt与apt-get命令的区别

    转载于https://www.sysgeek.cn/apt-vs-apt-get/ Ubuntu 16.04 发布时,一个引人注目的新特性便是 apt 命令的引入.其实早在 2014 年,apt 命令 ...

  5. apt与apt-get命令的区别与解释

    [apt与apt-get命令的区别与解释] Ubuntu 16.04 发布时,一个引人注目的新特性便是 apt 命令的引入.其实早在 2014 年,apt 命令就已经发布了第一个稳定版,只是直到 20 ...

  6. Linux快速目录间切换cd pushd popd

    1.   cd -     当前目录和之前所在的目录之间的切换 2.   cd + Alt . 用上次命令的最后一个目录路径 要用上上次命令的最后一个目录,就Alt+.两次就可以了 3.   push ...

  7. apt与apt-get命令的区别

    apt 和 apt-get的区别 Debian 作为 Ubuntu.Linux Mint 和 elementary OS 等 Linux 操作系统的母板,其具有强健的「包管理」系统,它的每个组件和应用 ...

  8. MVVM模式解析和在WPF中的实现(三)命令绑定

    MVVM模式解析和在WPF中的实现(三) 命令绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...

  9. Oracle学习笔记三 SQL命令

    SQL简介 SQL 支持下列类别的命令: 1.数据定义语言(DDL) 2.数据操纵语言(DML) 3.事务控制语言(TCL) 4.数据控制语言(DCL)  

随机推荐

  1. appearance格式化表单元素的边框,在chrome和FF下鼠标点击时会多出一个蓝色边框

    可在元素上添加样式 -webkit-appearance: none; -moz-appearance:none;outline:none; 清除掉元素所有的外貌,以便自定义风格

  2. PSP(4.6——4.12)以及周记录

    1.PSP 4.6 8:30 10:30 20 100 博客 B Y min 12:00 13:00 5 55 Account A Y min 13:05 13:15 0 10 站立会议 A Y mi ...

  3. Latex使用:在latex中添加算法模块

    在Miktex下有三个latex algorithm包,分别为:algorithm,algorithmic,algorithm2e三个,其中algorithm,algorithmic经常成套使用: l ...

  4. Java 策略模式(Strategy)

    创建一个能够根据所传递的参数对象的不同而具有不同行为的方法 要执行的算法固定不变,封装到一个类(Context)中 策略就是传递进去的参数对象,它包含执行代码 策略接口 /** * 策略接口 */ p ...

  5. BOM之screen对象

    前面的话 screen对象在javascript编程中,比较冷门,不太常用.screen对象用来表明客户端的能力,其中包括浏览器窗口外部的显示器的信息,如像素高度和宽度等.本文将详细介绍screen对 ...

  6. Java NIO 详解(一)

    一.基本概念描述 1.1 I/O简介 I/O即输入输出,是计算机与外界世界的一个借口.IO操作的实际主题是操作系统.在java编程中,一般使用流的方式来处理IO,所有的IO都被视作是单个字节的移动,通 ...

  7. spring boot 系列之六:深入理解spring boot的自动配置

    我们知道,spring boot自动配置功能可以根据不同情况来决定spring配置应该用哪个,不应该用哪个,举个例子: Spring的JdbcTemplate是不是在Classpath里面?如果是,并 ...

  8. Benefit UVA - 11889(已知LCM和其中一个数,求另一个数)

    首先对于C不能整除A的状况肯定排除 然后得到B=C/A 然后取G=GCD(A,B) 如果G==1,那么此时B就是解 否则的话,就证明A,B,的最小公倍数肯定不是C,因为其最小公倍数是A*B/G 那么我 ...

  9. Fib的奇怪定理 : gcd(F[n],F[m])=F[gcd(n,m)]

    引理1:gcd(F[n],f[n-1])=1 因为 F[n]=f[n-1]+F[n-2] 所以 gcd(F[n],f[n-1]) = gcd(F[n-1]+F[n-2],F[n-1]) gcd的更损相 ...

  10. UVAlive-7040 color(组合数学,二项式反演)

    链接:vjudge 题目大意:有一排方格共 $n$ 个,现在有 $m$ 种颜色,要给这些方格染色,要求相邻两个格子的颜色不能相同.现在问恰好用了 $k$ 种颜色的合法方案数.答案对 $10^9+7$ ...