#Bash 的 which 命令
which [OPTION]... NAME...
功能
在 PATH 环境变量中定位命令的位置。
类型
可执行文件(/usr/bin/which),属于 which。
参数
OPTION选项:-a- 打印所有匹配的路径;而非第一个匹配的路径-s- 不打印任何内容,仅通过返回值报告是否找到
NAME- 要定位的命令列表
返回值
- 0 - 所有命令都找到且可执行
- 1 - 存在命令找不到或不可执行
- 2 - 指定了无效的选项参数
#示例
$ which ls # 在 PATH 中查找 ls
/usr/bin/ls
$ which ls cat # 在 PATH 中查找 ls 和 cat
/usr/bin/ls
/usr/bin/cat
$ which -a ls cat # 打印所有匹配的路径
/usr/bin/ls
/bin/ls
/usr/bin/cat
/bin/cat
#相关命令
| 命令 | 说明 |
|---|---|
| whereis | 定位二进制文件、源码文件和手册文件 |
#推荐阅读
#手册
WHICH(1) General Commands Manual WHICH(1)
NAME
which - locate a command
SYNOPSIS
which [-as] filename ...
DESCRIPTION
which returns the pathnames of the files (or links) which would be exe‐
cuted in the current environment, had its arguments been given as com‐
mands in a strictly POSIX-conformant shell. It does this by searching
the PATH for executable files matching the names of the arguments. It
does not canonicalize path names.
OPTIONS
-a print all matching pathnames of each argument
-s silently return 0 if all of the executables were found or 1 oth‐
erwise
EXIT STATUS
0 if all specified commands are found and executable
1 if one or more specified commands is nonexistent or not exe‐
cutable
2 if an invalid option is specified
Debian 29 Jun 2016 WHICH(1)