Bash中的“哪个"与“命令-v" [英] 'which' vs 'command -v' in Bash

查看:118
本文介绍了Bash中的“哪个"与“命令-v"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读过一些bash常见问题解答(我不记得了),应该避免使用which并首选command -v.

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?

推荐答案

好吧...

command可能内置在您的Shell中,并且使用-v选项将告诉您您的Shell如何调用指定为其选项的命令.

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是位于/usr/bin/which的外部二进制文件,它逐步遍历$PATH环境变量并检查文件是否存在.

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.

选择前者而不是后者的原因是,它避免了对Shell外部内容的依赖.

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

这两个命令执行不同的操作,您应该选择一个更符合您需求的命令.例如,如果command内置在您的Shell中,则command -v command会通过其输出(通过不存在的路径)指示这一点,但是which command会尝试指向您路径上的文件,无论shell如何解释command.

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.

这篇关于Bash中的“哪个"与“命令-v"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆