zsh:解析 zsh 命令参数中使用的变量中的通配符?

zsh: parsing wildcards in variables used in arguments on zsh commands?

ls *.txt 显示名称以 .txt

结尾的所有文件

但是,如果我在 zsh shell 上执行以下操作:(在我的例子中是在 macOS 10.15 Catalina 上)

a=*.txt
b='*.txt'
c="*.txt"

# trying no quotes, single quotes, double quotes
# although doesn't make any difference here

ls $a
ls $b
ls $c
ls "$a"
ls "$b"
ls "$c"

我得到

ls: *.txt: No such file or directory

在所有情况下。如何在变量中包含通配符,然后让像 ls 这样的命令实际将其处理为通配符,而不是文字字符?

您应该在 $ 和变量名之间使用 ~(波浪号) 在 zsh 中执行 globbing。即

ls $~a

您可以使用以下命令在 zsh 中启用通配符:

unsetopt nomatch

如果您想永久更改,请将上面的命令放入您的 .zshrc 文件。