【问题标题】:How do I use to the zsh 'command' option to execute a the builtin 'source' command?如何使用 zsh 'command' 选项来执行内置的 'source' 命令?
【发布时间】:2021-01-10 22:30:59
【问题描述】:

每次我的 shell 获取文件时,我都会尝试记录。我正在使用zsh,所以我进入zshenv并添加了这个功能。

source() {
    echo "sourcing $1"
    command source $1
}

这个想法是每次“source [file]”出现在我的一个点文件中并被执行时,它应该首先将操作打印到终端,然后再实际获取文件。

相反,我遇到了一些这样的错误

sourcing /Users/js/.cargo/env
source:2: command not found: source
sourcing /Users/js/.sources/postgres-env.sh
source:2: command not found: source
sourcing /Users/js/.oh-my-zsh/oh-my-zsh.sh
source:2: command not found: source
sourcing /Users/js/.iterm2_shell_integration.zsh
source:2: command not found: source

在这里使用带有 zsh 的 shell 'command' 选项调用 source 的正确方法是什么?

【问题讨论】:

  • 在 zsh 中,command xx 解释为外部命令。您不能使用它运行内部命令。这是与 bash 不同的 AFIK。

标签: zsh


【解决方案1】:

command 旨在专门调用 external 命令。例如,如果您有 git 的别名或函数,command git 将绕过这些。

您正在寻找 builtin 命令以将命令查找限制为仅内置命令。

source() {
    echo "sourcing $1"
    builtin source "$1"
}

【讨论】:

    【解决方案2】:

    为了让它不受 shell 的影响,你可以使用它来代替:

    #!/usr/bin/env sh
    
    source() {
      echo "sourcing $1"
      . "$1"
    }
    
    source "$1"
    

    【讨论】:

    • 关闭,但不完全是 OP 想要的。请注意,在 zsh 中,source. 的行为略有不同,因此您的 source 函数不仅会记录正在发生的事情,而且与内置的 source 命令具有不同的语义。
    猜你喜欢
    • 2020-05-30
    • 2021-02-23
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    • 2016-09-04
    • 1970-01-01
    • 2013-10-13
    相关资源
    最近更新 更多