美文网首页
【*nux命令】which VS where VS wherei

【*nux命令】which VS where VS wherei

作者: 清水芦苇 | 来源:发表于2017-10-14 21:36 被阅读45次
which VS where VS whereis

这三个命令都是用来查找二进制程序的。下面介绍各自不同点。

whereis

man whereis是这么解释的

The whereis utility checks the standard binary directories for the specified programs, printing out the paths of any it finds.
The path searched is the string returned by the sysctl(8) utility for the ``user.cs_path'' string

它是检查标准二进制目录的,这个标准指的应该是系统环境变量中的,而非用户环境变量中的二进制。但是可以肯定它显示的不是真实的二进制不一定是真实触发的哪个二进制。

which

man which

locate a program file in the user's path

说的不是很详尽。which 命令用来检测用户环境变量中的二进制,也就是说这是真实触发的命令

where

在 mac 中 where命令会显示所有的相关命令。而 which 返回的是真实触发的二进制命令,where 返回系统环境变量中的二进制命令,一般也就是一个。

其他命令

还有一个不常用的命令——type
type 也是和检查二进制路径有关的命令。它其实不能算查找命令,而是用来区分某个命令到底是由shell自带的,还是由shell外部的独立二进制文件提供的。

其他知识

关于shell

shell 是一个命令语言解释器(command-language interpreter),是用户和Linux内核之间的接口程序。在提示符下输入的每个命令都由shell先解释然后传给Linux内核。Mac OS 不仅带了 sh、bash 这两个最基础的shell,还内置了 ksh、csh、zsh 等不常用的解释器。

为什么 unset shell 之后不能永久生效

shell 每次启动都是从文件加载环境变量,而unset操作只是把导入shell里面的变量删除掉,而不会对导入环境变量的文件进行操作。

shell 查找命令的顺序

环境变量`/usr/local/bin`和`/usr/bin`

如果两个 $PATH 中都有同名命令,shell会以哪个为主?
实际上,shell 会沿着 $PATH 的顺序进行搜索,先找到哪个就停止搜索并使用这个。/usr/local/bin/usr/bin前面,自然会被先使用。所以which vim显示的结果是/usr/local/bin/vim

参考资料

Linux的五个查找命令:find,locate,whereis,which,type
https://superuser.com/questions/430002/whereis-and-which-return-different-paths-in-mac-os-x

相关文章

网友评论

      本文标题:【*nux命令】which VS where VS wherei

      本文链接:https://www.haomeiwen.com/subject/exxfuxtx.html