[英]Why is accessing a command using the full path different than through just the command name?
I added node to my path.我在路径中添加了节点。 My understanding is that I'm calling the same command using the following:
我的理解是我正在使用以下命令调用相同的命令:
$ node --version
v10.24.1
$ which node
/usr/local/bin/node
$ /usr/local/bin/node --version
v14.17.0
My question is why the versions are different?我的问题是为什么版本不同?
which
will check the PATH
, but in Bash it is not aware of builtins, aliases, or functions. which
将检查PATH
,但在 Bash 中它不知道内置函数、别名或函数。 Instead, it is safer to use type
instead , which avoids these pitfalls.相反,使用
type
更安全,这样可以避免这些陷阱。
In your example, the most likely explanation is that node
is either an alias or a function.在您的示例中,最可能的解释是
node
是别名或 function。 type node
should give you a clue what gets executed, but apparently it is not the same binary as /usr/local/bin/node
. type node
应该给你一个线索什么被执行,但显然它与/usr/local/bin/node
不同的二进制文件。
As an alternative, you can enable logging:作为替代方案,您可以启用日志记录:
$ set -x
$ node
If it is an alias or function, you should see it in the output.如果它是别名或 function,您应该在 output 中看到它。
Here is a contrived example to demonstrate the difference between type
and which
(executed on Arch Linux):这是一个人为的示例来演示
type
和which
之间的区别(在 Arch Linux 上执行):
$ node --version
v16.2.0
$ node () {
echo "v99.9.9"
}
$ node --version
v99.9.9
$ which node
/usr/bin/node
$ /usr/bin/node --version
v16.2.0
$ type node
node is a function
node ()
{
echo "v99.9.9"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.