简体   繁体   English

为什么使用完整路径访问命令与仅通过命令名称不同?

[英]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):这是一个人为的示例来演示typewhich之间的区别(在 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.

相关问题 如何在不使用完整路径的情况下远程永久执行命令? - How to execute forever command remotely without using full path? bash:节点:找不到命令,但完整路径有效; 我怎样才能使“ node -v”工作? (node.js) - bash: node: command not found, but full path is working; how can I make just “node -v” work? (node.js) Laravel命令输出的节点版本与命令行不同 - Laravel Command outputs different version of node than command line 为什么为我的npm软件包生成的命令脚本与其他软件包不同? - why the command script generated for my npm package is different than other package? Meyda 特征提取的数据在命令行和通过 nodejs 不同 - Meyda feature extracted dat is different in command line and through nodejs 终端命令“ --version”显示的版本比我刚安装的版本早 - Terminal command '--version' is showing an earlier version than what I just installed 如何在整个项目中将自定义模块导出为全局模块,并通过仅提供名称而不是 js 中的路径来导入? - How to export custom modules as global across the project and import by just providing the name rather than path in js? 是否可以使用tsc命令将单个TypeScript文件编译到输出路径? - Is it possible to compile a single TypeScript file to an output path using the tsc command? 为什么“ sh -c”返回与实际命令不同的结果? - Why is 'sh -c' returning a different result from my actual command? 节点命令和文件名 - node command and file name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM