'which' vs 'command -v' in Bash [duplicate]

[亡魂溺海] 提交于 2019-12-31 09:44:10

问题


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

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


回答1:


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.

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.

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.



来源:https://stackoverflow.com/questions/37056192/which-vs-command-v-in-bash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!