zsh - 处理传递给 ZSH 函数的 "?"字符

标签 zsh zshrc zsh-alias

我在 ZSH 中设置简单函数时遇到问题。

我想制作只从 youtube 下载 mp3 文件的功能。

我使用了 youtube-dl,我想做一些简单的功能来让我更容易

ytmp3(){
  youtube-dl -x --audio-format mp3 "$@"}

所以当我尝试

ytmp3 https://www.youtube.com/watch?v=_DiEbmg3lU8

我明白了

zsh: no matches found: https://www.youtube.com/watch?v=_DiEbmg3lU8

但如果我尝试

ytmp3 "https://www.youtube.com/watch?v=_DiEbmg3lU8"

它有效。 我发现如果我删除所有字符后程序运行(但不会下载任何东西)?包括它。所以我猜这是 zsh 的某种特殊字符。

最佳答案

默认情况下,ZSH 将尝试“glob”您在命令行上使用的模式(它将尝试将模式与文件名匹配)。如果它无法匹配,您将收到您遇到的错误(“未找到匹配项”)。

您可以通过禁用 nomatch 选项来禁用此行为:

unsetopt nomatch

手册页对此选项的描述如下(它描述了启用该选项时发生的情况):

If a pattern for filename generation has no matches, print an error, instead of leaving it unchanged in the argument list.

禁用该选项再试一次:

$ unsetopt nomatch
$ ytmp3 https://www.youtube.com/watch?v=_DiEbmg3lU8

如果您想永久禁用该选项,可以将禁用命令添加到您的 ~/.zshrc 文件中。

关于zsh - 处理传递给 ZSH 函数的 "?"字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35609127/

相关文章:

bash - 这个单行脚本的哪一部分是特定于 zsh 的?

vim - 如何让 zsh 提示 vi 模式使用我的 .vimrc?

Zsh 将命令的输出读入换行符的数组拆分中

bash - 从 zsh 进入 bash 时如何加载 ~/.bash_profile?

zsh - 我的 zsh 提示不是粗体

zsh - 如何在$ XDG_CONFIG_HOME中进行zsh搜索配置

bash - 如何在远程服务器上为 git commit、git pull、git push 和 pull 创建 bash 或 zsh 别名?

bash - 如何从 shell 获取一组子进程(在 macOS 中)的所有父进程名称?