Bash 中的“which”与“command -v”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37056192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:17:03  来源:igfitidea点击:

'which' vs 'command -v' in Bash

bashwhich

提问by adrelanos

I've read in some bash FAQ a while ago (that I don't remember), that whichshould be avoided and command -vpreferred.

我之前读过一些 bash 常见问题解答(我不记得了),which应该避免和command -v首选。

Why is that so? What are the advantages, disadvantages of either one?

为什么呢?任何一种都有哪些优点和缺点?

回答by ghoti

Well...

好...

commandis likely built in to your shell, and with the -voption will tell you how your shell will invoke the command specified as its option.

command很可能内置在您的 shell 中,并且该-v选项会告诉您您的 shell 如何调用指定为其选项的命令。

whichis an external binary, located at /usr/bin/whichwhich steps through the $PATHenvironment variable and checks for the existence of a file.

which是一个外部二进制文件,位于/usr/bin/which它遍历$PATH环境变量并检查文件是否存在的位置。

A reason to select the former over the latter is that it avoids a dependency on something that is outside your shell.

选择前者而不是后者的一个原因是它避免了对外壳之外的东西的依赖。

The two commands do different things, and you should select the one that more closely matches your needs. For example, if commandis built in to your shell, command -v commandwill indicate this with its output (through the non-existence of path), but which commandwill try to point to a file on your path, regardless of how commandwould be interpreted by your shell.

这两个命令执行不同的操作,您应该选择更符合您需求的命令。例如,如果command内置于您的外壳程序中,command -v command将通过其输出(通过不存在的路径)指示这一点,但which command将尝试指向您路径上的文件,而不管command您的外壳程序如何解释。