使用 'command' 内置 Bash shell

Use of the 'command' built-in in Bash shell

内置的'command'bashshell有什么用? This 页面说它抑制了 shell 函数查找,但我不确定这是什么意思。你能解释一下或举个例子吗?

让我们举一个简单的函数例子。我们要确保 cp 始终使用 -i 选项。我们可以使用别名来做到这一点,但别名很简单,您无法在其中构建太多智能。功能更强大

我们可以试试这个(记住,这只是一个简单的例子):

cp() {
    cp -i "$@"
}

cp gash.txt gash2.txt

这给了我们无限递归!它只是不断地调用自己。在这种情况下我们可以使用 /bin/cp,但这是 command 的用途:

cp() {
    command cp -i "$@"
}

cp gash.txt gash2.txt

有效,因为现在它忽略了 cp 函数。

观察:

$ date() { echo "This is not the date"; }
$ date
This is not the date
$ command date
Tue Aug  2 23:54:37 PDT 2016