which
进程与系统
用法
which [-a] <name>在 PATH 里查找同名可执行文件在哪;因为它只看 PATH,对别名、函数和 shell 内建命令会给出错误答案,老实的答案来自 `type -a` 或 `command -v`。
常用选项
| 选项 | 含义 |
|---|---|
| -a | Show every match in PATH, not just the first one. |
| <name> <name> | Several names at once; the exit status is nonzero if any is missing. |
| -s | Print nothing, just set the exit status (BSD/macOS). |
示例
which -a python3Spot a second python3 earlier in PATH than the one you expected.
command -v node || echo missingThe portable check to use in scripts.
看什么在跑、什么在吃机器。先弄清是什么,再决定杀不杀。
怎么看
- 方括号 [ ] 表示这部分可以不写。
- 省略号 … 表示可以写多个。
- 选项区分大小写——有些命令里 -r 和 -R 不是一回事。
常见问题
Q. which 是做什么的?
在 PATH 里查找同名可执行文件在哪;因为它只看 PATH,对别名、函数和 shell 内建命令会给出错误答案,老实的答案来自 `type -a` 或 `command -v`。
Q. 怎么写?
which [-a] <name> —— 方括号表示可以省略的部分。
Q. 值得记的选项有几个?
这里列了 3 个,完整列表在 man which。这条命令属于进程与系统。
相关命令
man 手册: man which