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
'which' vs 'command -v' in Bash
提问by adrelanos
I've read in some bash FAQ a while ago (that I don't remember), that which
should be avoided and command -v
preferred.
我之前读过一些 bash 常见问题解答(我不记得了),which
应该避免和command -v
首选。
Why is that so? What are the advantages, disadvantages of either one?
为什么呢?任何一种都有哪些优点和缺点?
回答by ghoti
Well...
好...
command
is likely built in to your shell, and with the -v
option will tell you how your shell will invoke the command specified as its option.
command
很可能内置在您的 shell 中,并且该-v
选项会告诉您您的 shell 如何调用指定为其选项的命令。
which
is an external binary, located at /usr/bin/which
which steps through the $PATH
environment 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 command
is built in to your shell, command -v command
will indicate this with its output (through the non-existence of path), but which command
will try to point to a file on your path, regardless of how command
would be interpreted by your shell.
这两个命令执行不同的操作,您应该选择更符合您需求的命令。例如,如果command
内置于您的外壳程序中,command -v command
将通过其输出(通过不存在的路径)指示这一点,但which command
将尝试指向您路径上的文件,而不管command
您的外壳程序如何解释。