RocketNuts
RocketNuts

Reputation: 11140

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

ls *.txt shows all files whose name ends with .txt

However if I do the following on a zsh shell: (on macOS 10.15 Catalina in my case)

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"

I'm getting

ls: *.txt: No such file or directory

in all cases. How do I include wildcards in a variable and then have commands like ls actually process it as wildcards, rather than literal characters?

Upvotes: 4

Views: 1002

Answers (2)

Tanel Tammik
Tanel Tammik

Reputation: 17149

You can enable wildcards in zsh by using the command:

unsetopt nomatch

If you want to make the change permanent, put the command above into your .zshrc file.

Upvotes: 1

Kulfy
Kulfy

Reputation: 189

You should use ~(tilde) between $ and variable name to perform globbing in zsh. That is

ls $~a

Upvotes: 3

Related Questions