首页 > 解决方案 > zsh:解析zsh命令参数中使用的变量中的通配符?

问题描述

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: 没有这样的文件或目录

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

标签: shellvariablesterminalzshwildcard

解决方案


您应该在和变量名~之间使用(波浪号)$在 zsh 中执行通配符。那是

ls $~a

推荐阅读