在 PATH 中运行与现有函数同名的可执行文件

Pet*_*etr 17 bash path function

有时我会定义一个函数来隐藏可执行文件并调整其参数或输出。因此该函数与可执行文件具有相同的名称,我需要一种如何从该函数运行可执行文件而不递归调用该函数的方法。例如,要自动运行的输出fossil diff通过colordiffless -R使用:

function fossil () {
    local EX=$(which fossil)
    if [ -z "$EX" ] ; then
        echo "Unable to find 'fossil' executable." >&2
        return 1
    fi
    if [ -t 1 ] && [ "$1" == "diff" ] ; then
        "$EX" "$@" | colordiff | less -R
        return
    fi
    "$EX" "$@"
}
Run Code Online (Sandbox Code Playgroud)

如果我确定可执行文件的位置,我可以简单地输入/usr/bin/fossil. Bash 认识到这/意味着它是一个可执行文件,而不是一个函数。但由于我不知道确切的位置,我不得不求助于调用which并检查结果。有没有更简单的方法?

man*_*ork 21

使用commandshell 内置:

bash-4.2$ function date() { echo 'at the end of days...'; }

bash-4.2$ date
at the end of days...

bash-4.2$ command date
Mon Jan 21 16:24:33 EET 2013

bash-4.2$ help command
command: command [-pVv] command [arg ...]
    Execute a simple command or display information about commands.

    Runs COMMAND with ARGS suppressing  shell function lookup, or display
    information about the specified COMMANDs.  Can be used to invoke commands
    on disk when a function with the same name exists.
Run Code Online (Sandbox Code Playgroud)

  • @jordanm,仅适用于别名。问题是关于函数的。http://pastebin.com/TgkHQwbb (4认同)
  • 另一种选择是转义命令`\date`。 (2认同)