三者区别
| 工具 |
找什么 |
which |
看 $PATH 里某命令在哪 |
whereis |
找命令 + man page + 源码 |
type |
shell 内置:详细告诉你这是别名 / 函数 / 内置 / 外部 |
which:最常用
which python3
# /usr/bin/python3
which ls
# /usr/bin/ls
which -a python # 列出 $PATH 里所有同名
# /usr/local/bin/python
# /usr/bin/python
whereis:含文档
whereis ls
# ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
type:shell 内置(推荐)
type cd
# cd is a shell builtin
type ls
# ls is aliased to 'ls --color=auto'
type python
# python is /usr/bin/python
type -a python # 列所有
# python is /usr/local/bin/python
# python is /usr/bin/python
which 看不到别名和 shell 内置——它是独立程序,不知道当前 shell 状态。type 才是完整真相。
实战
# 排查"为什么我跑的不是预期的版本"
which python # /usr/bin/python ← 老版本
type -a python # 看完整候选
# 看 $PATH 顺序
echo $PATH | tr ':' '\n'
# /usr/local/bin ← 先找这
# /usr/bin
# 调整 $PATH 让自定义版本优先
export PATH="/opt/myapp/bin:$PATH"
坑
which 在某些发行版是 shell 内置(bash 自带 which),有些是独立 /usr/bin/which——行为略不同
- 真要看是不是别名 / 函数用
type