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

# Bash 的 pushd 命令

pushd [-n] [+N | -N | DIR]

!subtitle:功能

将当前工作目录压入栈顶,然后将工作目录切换到 DIR

如果不带 DIR 参数,则将当前工作目录与原先栈顶的目录交互。

!subtitle:类型

Bash 内置命令。

!subtitle:参数

  • -n - 仅将当前工作目录压入栈顶,不切换工作目录

  • +N - 旋转目录栈,使得从栈顶向下第的 N 个目录位于栈顶

  • -N - 旋转目录栈,使得从栈底向上第的 N 个目录位于栈顶

  • DIR - 目标目录

# 示例

$ pwd                   # 查看当前目录
/home/user/primers
$ pushd /tmp            # 将 /home/user/primers 入栈,切换到 /tmp
$ pwd                   # 查看当前目录
/tmp
$ pushd /usr/share      # 将 /tmp 入栈,切换到 /usr/share
$ pwd                   # 查看当前目录
/usr/share
$ dirs                  # 查看当前工作目录和目录栈
/usr/share /tmp ~
  • 波浪号(~)表示用户主目录

# 相关命令

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

# 手册

NAME
    pushd - Add directories to stack.

SYNOPSIS
    pushd [-n] [+N | -N | DIR]

DESCRIPTION
    Add directories to stack.
    
    Adds a directory to the top of the directory stack, or rotates
    the stack, making the new top of the stack the current working
    directory.  With no arguments, exchanges the top two directories.
    
    Options:
      -n	Suppresses the normal change of directory when adding
    		directories to the stack, so only the stack is manipulated.
    
    Arguments:
      +N	Rotates the stack so that the Nth directory (counting
    		from the left of the list shown by `dirs', starting with
    		zero) is at the top.
    
      -N	Rotates the stack so that the Nth directory (counting
    		from the right of the list shown by `dirs', starting with
    		zero) is at the top.
    
      dir	Adds DIR to the directory stack at the top, making it the
    		new current working directory.
    
    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