Answers:
因为只有内置程序才能完全了解别名,Shell函数,其他内置程序等等,所以只有外壳程序内置程序才能正确告诉外壳程序给定命令名的功能。切记:并非所有命令最初都与可执行文件相对应。
对于Bourne Again Shell的,bash,内置的是type命令:
$ type '['
[ is a shell builtin
对于鱼贝,fish,的type内置工程庆典相似。要获取可执行文件的路径,请使用command -v:
$ type cat
cat is /bin/cat
$ command -v cat
/bin/cat
对于Korn shell中,ksh,内置的是whence命令-与type最初设置为一个普通的别名whence -v和command内置与-v相当于选项whence:
$ whence -v ls
ls is a tracked alias for /bin/ls
对于在Z壳牌zsh,内置的是whence命令与command内置与-v等效选项whence和内置插件type,which和where相当于whence与期权-v,-c和-ca分别。
$ whence ls
/bin/ls
对于TC壳牌tcsh,内置的是which命令-不与该名称的任何外部命令混淆:
> which ls
ls: aliased to ls-F
> which \ls
/bin/ls
type也内置到zsh吗?
which-2.19-6.el6.x86_64)放入启动文件/etc/profile.d/which2.sh。这是由非csh shell(包括zsh)读取的。它是别名which。因此,特别是对于zsh用户,它覆盖了内置功能which。
您可以which为此使用:
aix@aix:~$ which ls
/bin/ls
它通过搜索PATH与参数名称匹配的可执行文件来工作。请注意,这不适用于shell别名:
aix@aix:~$ alias listdir=/bin/ls
aix@aix:~$ listdir /
bin dev initrd.img lib32 media proc selinux tmp vmlinuz
...
aix@aix:~$ which listdir
aix@aix:~$
type但是,确实可以:
aix@aix:~$ type listdir
listdir is aliased to `/bin/ls'
which本身并不一定足够。它仅返回$ PATH中找到的第一个名称。该名称可能是符号链接,而不是实际的链末可执行文件。
which不不(一定)返回的可执行文件。它返回它在$ PATH中找到的第一个匹配文件名(或使用时,多个类似命名的文件which -a)... 实际的可执行文件可能有多个链接。
which locate/usr/bin/locatefile $(which locate)/usr/bin/locate: symbolic link to /etc/alternatives/locate' 查找实际可执行文件的命令是readlink -e,
(与结合使用which)
readlink -e $(which locate)/usr/bin/mlocate 要查看所有中间链接:
f="$(which locate)" # find name in $PATH
printf "# %s\n" "$f"
while f="$(readlink "$f")" ;do # follow links to executable
printf "# %s\n" "$f"
done
# /usr/bin/locate
# /etc/alternatives/locate
# /usr/bin/mlocate
你可以试试:
whereis ls
它给了我:
ls: /bin/ls /usr/share/man/man1/ls.1.gz