# PATH（变量）

**PATH** 是 Unix 及类 Unix 操作系统上的一个环境变量，它保存一串以冒号分隔的目录列表；当 shell 和许多其他程序被要求运行一个以裸文件名（即不含斜杠的名称）给出的可执行文件时，会按顺序搜索这些目录。<sup>[1](https://www.man7.org/linux/man-pages/man7/environ.7.html)</sup> 它是最广泛使用的环境变量之一：设置它决定了用户可以通过只输入名称来启动哪些程序，以及同名程序按什么顺序被选中。

| 关键事实 | 详情 |
|---|---|
| 用途 | sh(1) 及许多其他程序用来查找以不含斜杠的文件名指定的可执行文件的目录前缀序列<sup>[1](https://www.man7.org/linux/man-pages/man7/environ.7.html)</sup> |
| 格式 | 前缀以冒号分隔；从头到尾搜索，直到找到具有适当权限的可执行文件<sup>[2](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap08.html)</sup> |
| 空前缀 | 零长度前缀（两个相邻冒号，或开头、结尾的冒号）是表示当前工作目录的遗留方式；它已被弃用，POSIX 要求使用诸如 `.` 这样的显式路径名<sup>[1](https://www.man7.org/linux/man-pages/man7/environ.7.html)</sup> |
| 回退默认值 | 如果 PATH 未设置，OpenBSD 的 `execvp` 系列函数使用 `_PATH_DEFPATH`：`/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11R6/bin:/usr/local/bin:/usr/local/sbin`<sup>[3](https://man.openbsd.org/execvp.3)</sup> |
| 历史默认值 | `execvp` 和 `execlp` 的历史默认值为 `.:/bin:/usr/bin`，后为提高安全性和改善行为而更改<sup>[3](https://man.openbsd.org/execvp.3)</sup> |
| Shell 缓存 | Bash 在哈希表中记住命令位置，仅当在那里找不到命令时才执行完整的 `$PATH` 搜索；对 PATH 赋值会清除所有已哈希的文件名<sup>[4](https://www.gnu.org/software/bash/manual/html_node/Command-Search-and-Execution.html)</sup><sup> • </sup><sup>[5](https://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html)</sup> |
| Windows 对应物 | Windows 的 `path` 命令设置 PATH 环境变量中搜索可执行文件（.exe）的目录<sup>[6](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/path)</sup> |

## 搜索的工作方式

当命令名不含斜杠时，shell 依次搜索每个 PATH 前缀，将前缀、一个斜杠和文件名拼接成候选路径名，并在遇到第一个具有适当执行权限的可执行文件时停止。<sup>[2](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap08.html)</sup> 因此顺序很重要：列表中靠前的命令会遮蔽靠后的同名命令。POSIX shell 规范对命令搜索和执行要求相同的行为，而 `command -p` 实用程序使用一个保证能找到所有标准实用程序的默认 PATH 来执行搜索。<sup>[7](https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html)</sup><sup> • </sup><sup>[8](https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/utilities/command.html)</sup>

搜索并不限于 shell。当给定的文件名不含斜杠时，C 库函数 `execvp`、`execvpe` 和 `execlp` 会使用环境中的 PATH 复现 shell 的搜索。<sup>[3](https://man.openbsd.org/execvp.3)</sup> 有两种错误情况被特殊处理：如果某个候选文件权限被拒绝（`EACCES`），这些函数会继续搜索路径的其余部分，仅当其他什么也没找到时才报告 `EACCES`；如果文件的头部无法识别（`ENOEXEC`），它们会以该文件的路径作为第一个参数来执行 shell，没有可识别二进制格式的脚本就是这样被运行的。<sup>[3](https://man.openbsd.org/execvp.3)</sup>

**Shell 在搜索之上增加了缓存。** Bash 定位命令时，先检查 shell 函数，再检查内建命令，只有到那时，如果名称不含斜杠，才搜索 `$PATH` 的每个元素；它把完整路径名记录在哈希表中，以便重复调用时跳过搜索，只有哈希未命中时才进行完整搜索。<sup>[4](https://www.gnu.org/software/bash/manual/html_node/Command-Search-and-Execution.html)</sup> 对 PATH 变量赋值会清除所有已哈希的文件名，因此更改后的 PATH 对后续查找立即生效。<sup>[5](https://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html)</sup> zsh 行为类似，并维护一对绑定的变量：`$path`，一个其元素可分别操作的数组，以及 `$PATH`，其他命令看到的导出的冒号连接字符串；当 `$path` 被修改时 zsh 会自动重新哈希，因此放在列表靠前位置的新命令无需手动干预即可被找到。<sup>[9](https://zsh.sourceforge.io/Guide/zshguide03.html)</sup>

## 设置与继承 PATH

PATH 是普通环境变量，因此会被子进程继承：在 shell 中导出的值会被复制到该 shell 启动的每个进程所接收的环境块中。<sup>[10](https://sigilipelli.github.io/shell-mastery-path/level-2/08-environment-config/)</sup>

初始值来自若干地方。在使用 PAM 的 Linux 系统上，`pam_env` 模块在用户会话开始时从 `/etc/environment` 文件读取默认 PATH 设置，这就是为什么 shell 重启后该变量的内容会重新出现；在 [Windows Subsystem for Linux](https://www.edgepedia.org/zh/windows-subsystem-for-linux) 下，路径条目还可能通过 `appendWindowsPath` 互操作特性从 Windows 主机继承。<sup>[11](https://askubuntu.com/questions/1405726/how-does-path-get-its-contents-back-after-i-restart-the-shell)</sup> Linux 的 `environ(7)` 手册描述了同样的机制：初始环境由登录时经 `pam_env` 处理的 `/etc/environment`，加上系统级和每用户的 shell 初始化脚本填充。<sup>[1](https://www.man7.org/linux/man-pages/man7/environ.7.html)</sup>

在 Windows 上，`path` 命令设置搜索 `.exe` 文件的目录，将其放入 Autoexec.nt 文件可使操作系统在每次登录时追加指定的 MS-DOS 子系统搜索路径。<sup>[6](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/path)</sup>

## 安全考量

由于搜索在第一个匹配处停止，PATH 中靠前且攻击者可写入的目录会让同名程序遮蔽系统实用程序。这就是为什么在 Linux 上当前目录默认不在 PATH 中，也是为什么空前缀约定被弃用：POSIX 指出，符合标准的应用程序应使用诸如 `.` 这样的显式路径名来运行当前工作目录中的程序。<sup>[1](https://www.man7.org/linux/man-pages/man7/environ.7.html)</sup> OpenBSD 手册记录了库层面的同样历史：历史默认搜索路径 `.:/bin:/usr/bin` 首先搜索当前目录，后为提高安全性和改善行为而更改。<sup>[3](https://man.openbsd.org/execvp.3)</sup> POSIX 的理由说明还补充道，许多历史实现使用 `/bin` 和 `/usr/bin` 的默认值，而许多历史 Bourne shell 不将结尾冒号解释为当前目录，因此不符合标准。<sup>[12](https://pubs.opengroup.org/onlinepubs/9799919799/xrat/V4_xbd_chap01.html)</sup>

## 相关变量与工具

PATH 是路径搜索变量家族中最知名的成员。类似地，`CDPATH` 被某些 shell 用来查找 `cd` 命令的目标，`MANPATH` 被 `man` 用来查找手册页。<sup>[1](https://www.man7.org/linux/man-pages/man7/environ.7.html)</sup> CDPATH 变量本身可追溯到 System III（约 1980 年），是那个时代的显著新增之一。<sup>[13](https://www.oreilly.com/library/view/bash-pocket-reference/9781449388669/ch01s02.html)</sup>

Shell 还提供检查搜索的工具。POSIX 的 `command -v` 实用程序写出 shell 将用来调用某个命令名的路径名或命令，通过 PATH 找到的命令以绝对路径名写出。<sup>[8](https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/utilities/command.html)</sup> 在 fish 中，`path filter -fx` 命令打印 `$PATH` 中各目录下的所有可执行文件，展示 shell 可以运行的命令，而 fish 的 `path` 内建命令提供诸如 `basename`、`resolve` 和 `sort` 等子命令，用于一般性地操作路径字符串。<sup>[14](https://fishshell.com/docs/current/cmds/path.html)</sup>

## References

1. [environ(7) - Linux manual page](https://www.man7.org/linux/man-pages/man7/environ.7.html)
2. [Environment Variables, POSIX.1-2024 XBD Chapter 8](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap08.html)
3. [execvp(3) - OpenBSD manual pages](https://man.openbsd.org/execvp.3)
4. [Command Search and Execution, Bash Reference Manual](https://www.gnu.org/software/bash/manual/html_node/Command-Search-and-Execution.html)
5. [Bourne Shell Builtins, Bash Reference Manual](https://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html)
6. [path - Microsoft Learn, Windows Commands](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/path)
7. [Shell Command Language, POSIX.1-2024 XCU Chapter 2](https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html)
8. [command, POSIX.1-2024 Utilities](https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/utilities/command.html)
9. [A User's Guide to the Z-Shell, Chapter 3](https://zsh.sourceforge.io/Guide/zshguide03.html)
10. [Environment & Configuration Management, Shell Mastery Path](https://sigilipelli.github.io/shell-mastery-path/level-2/08-environment-config/)
11. [How does PATH get its contents back after I restart the shell?, Ask Ubuntu](https://askubuntu.com/questions/1405726/how-does-path-get-its-contents-back-after-i-restart-the-shell)
12. [Rationale for Base Definitions, POSIX.1-2024](https://pubs.opengroup.org/onlinepubs/9799919799/xrat/V4_xbd_chap01.html)
13. [History, bash Pocket Reference, O'Reilly](https://www.oreilly.com/library/view/bash-pocket-reference/9781449388669/ch01s02.html)
14. [path - manipulate and check paths, fish-shell 4.9.3 documentation](https://fishshell.com/docs/current/cmds/path.html)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Operating systems › Unix-like and Linux › Unix, BSD, and kernel internals

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/path-variable

English original: https://www.edgepedia.org/path-variable
