-1
$ touch fux fax fix
$ ls f[!a]x
zsh: event not found: a]x

I want to see fix and fux as output, but not fax.

I want to know the exact reason behind this issue. Is this particular issue regarding the code or not?

1
  • The old curse of history expansion! Almost nobody ever uses it, but almost everyone sometimes gets confused by it. setopt nobanghist in everybody's default ~/.zshrc would make this world a happier place.
    – Philippos
    Aug 30, 2021 at 13:37

1 Answer 1

2

Comments from OP have explained that they are looking to match all files OTHER than fax.

This can be fixed with shopt -s extglob (See Exclude one pattern from glob match)

$ touch fix fax fux
$ shopt -s extglob
$ ls f!(a)x
fix  fux

Alternate answer from Giles (comment below) that does not require extglob to be set:

$ ls f[^a]x
fix fux

My original answer text is below. This answer was written under the assumption he was attempting to use an exclamation point in his answer. After learning from what was stated in OP's comment on my answer, I have reviewed it and added the answer he was looking for above.

The ! represents a history lookup. See Can't use exclamation mark (!) in bash? Your shell will look for the last command that started with:

a]x

But there is not a command that starts with a]x.

Try escaping the exclamation point:

$ touch fix fax fux
$ ls f[\!a]x
fix  fux
7
  • i need fix and fux as output but ur code gives me fax Aug 30, 2021 at 13:36
  • @midhunmohan I have added to my answer explaining how to solve your problem. If it works, please accept the answer by clicking the green checkmark. Aug 30, 2021 at 13:47
  • 1
    Why go through such complicated lengths? The simple solution is to use f[^a]x. Unlike other sh-like shells, zsh supports ^ at the beginning of a character set to take the complement of the set. Aug 30, 2021 at 18:55
  • @Gilles'SO-stopbeingevil' I was under the impression that ^ represented "this character is at the beginning of the line". However, I see that I was wrong. Blame the coffee not kicking in early enough ;) Aug 30, 2021 at 18:57
  • 1
    @ilkkachu I thought it was more common, but indeed it isn't. POSIX requires ! to work and leaves ^ unspecified. Aug 31, 2021 at 14:25

Not the answer you're looking for? Browse other questions tagged .