Steve
Steve

Reputation: 31

How to use exclusive wildcard in zsh?

I am using Mac OS Big Sur 11.4 and recently switched to zsh, but I got some trouble using wildcard in it. Suppose I have a directory with files 1 2 3 1file 2file file1 file2 file3 and I want to list the files not starting with numbers. In bash it works fine as follows

Steves-Mac:test hengyuan$ cd test/dir3/
Steves-Mac:dir3 hengyuan$ ls
1     1file 2     2file 3     file1 file2 file3
Steves-Mac:dir3 hengyuan$ ls [[:digit:]]*
1     1file 2     2file 3
Steves-Mac:dir3 hengyuan$ ls [![:digit:]]*
file1 file2 file3

However, I got the following results in zsh

➜  dir3 ls
1     1file 2     2file 3     file1 file2 file3
➜  dir3 ls [[:digit:]]*
1     1file 2     2file 3
➜  dir3 ls [![:digit:]]*
zsh: event not found: [

Why did I get such strange results and how to fix them? Thank you.

Upvotes: 1

Views: 368

Answers (1)

Casper
Casper

Reputation: 34318

With zsh ! invokes history expansion. You can use [^...] instead which means the same thing as [!...]. You can also disable history expansion by either quoting [\!...] or using the special sequence !" which disables history expansion fully for the current command line.

So these are all equivalent:

ls [^[:digit:]]*
ls [\![:digit:]]*
ls !" [![:digit:]]*

To completely disable history expansion you can run setopt nobanghist.

Upvotes: 1

Related Questions