which (which) 命令的做用是在PATH變量指定的路徑中搜索某個系統命令的位置而且返回第一個搜索結果。也就是說使用which命令就能夠看到某個系統命令是否存在以及執行的究竟是哪個位置的命令java
/usr/bin/which [options] [--] COMMAND [...]
--version, -[vV] 顯示版本信息 --help, 顯示幫助信息 --skip-dot Skip directories in PATH that start with a dot. --skip-tilde Skip directories in PATH that start with a tilde. --show-dot Don't expand a dot to current directory in output. --show-tilde Output a tilde for HOME directory for non-root. --tty-only Stop processing options on the right if not on tty. --all, -a 查找所有匹配的,而非第一個
--read-alias, -i Read list of aliases from stdin. --skip-alias Ignore option --read-alias; don't read stdin. --read-functions Read shell functions from stdin. --skip-functions Ignore option --read-functions; don't read stdin.
[root@oracledb ~]# which java /usr/bin/java
[root@oracledb ~]# echo $PATH /usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@oracledb ~]# which which alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde' /usr/bin/which
用 which 去找出 which,結果會有兩個 which ,其中一個是 alias 這就是所謂的『命令別名』shell
[root@oracledb ~]# which cd /usr/bin/which: no cd in (/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
cd 這個經常使用的命令找不到,這是由於 cd 是bash 內建的命令! 可是 which 默認是找 PATH 內所規範的目錄,因此固然找不到!bash