Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 272
RocketNuts Avatar asked Sep 10 '25 14:09

RocketNuts


1 Answers

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

ls $~a
like image 115
Kulfy Avatar answered Sep 12 '25 05:09

Kulfy