国际访客建议访问 Primers 编程伙伴 国际版站点 > Bash 教程 > popd 以获得更好的体验。

# Bash 的 popd 命令

popd

!subtitle:功能

将目录栈的栈顶移除,并将工作目录切换为该目录。

!subtitle:类型

Bash 内置命令。

# 示例

$ pwd                   # 查看当前目录
/home/user/primers
$ pushd /tmp            # 将 /home/user/primers 入栈,切换到 /tmp
$ pwd                   # 查看当前目录
/tmp
$ pushd /usr/share      # 将 /tmp 入栈,切换到 /usr/share
$ pwd                   # 查看当前目录
/usr/share
$ popd                  # 出栈并切换回栈顶目录
$ pwd                   # 查看当前目录
/tmp
$ popd                  # 出栈并切换回栈顶目录
$ pwd                   # 查看当前目录
/home/user/primers

# 相关命令

命令 说明
cd 切换工作目录
pushd 将当前工作目录压入栈顶,并切换工作目录
dirs 查看当前工作目录和目录栈

# 手册

NAME
    popd - Remove directories from stack.

SYNOPSIS
    popd [-n] [+N | -N]

DESCRIPTION
    Remove directories from stack.
    
    Removes entries from the directory stack.  With no arguments, removes
    the top directory from the stack, and changes to the new top directory.
    
    Options:
      -n	Suppresses the normal change of directory when removing
    		directories from the stack, so only the stack is manipulated.
    
    Arguments:
      +N	Removes the Nth entry counting from the left of the list
    		shown by `dirs', starting with zero.  For example: `popd +0'
    		removes the first directory, `popd +1' the second.
    
      -N	Removes the Nth entry counting from the right of the list
    		shown by `dirs', starting with zero.  For example: `popd -0'
    		removes the last directory, `popd -1' the next to last.
    
    The `dirs' builtin displays the directory stack.
    
    Exit Status:
    Returns success unless an invalid argument is supplied or the directory
    change fails.

SEE ALSO
    bash(1)

IMPLEMENTATION
    GNU bash, version 5.0.17(1)-release (x86_64-redhat-linux-gnu)
    Copyright (C) 2019 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
本文 更新于: 2025-11-27 09:38:15 创建于: 2025-11-27 09:38:15