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=*.txtb='*.txt'c="*.txt"# trying no quotes, single quotes, double quotes# although doesn't make any difference herels $als $bls $cls "$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?

2

Best Answer


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

ls $~a

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.