-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?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • 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 '21 at 13:37

1 Answers1

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
  • i need fix and fux as output but ur code gives me fax – midhun mohan Aug 30 '21 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. – blackbrandt Aug 30 '21 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. – Gilles 'SO- stop being evil' Aug 30 '21 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 ;) – blackbrandt Aug 30 '21 at 18:57
  • @Gilles'SO-stopbeingevil', is it that common to not support [^a]? As far as I tested, Bash, Dash, ksh, Busybox, and yash all support it too; mksh was the one sh-like I found that didn't seem to. (Fish doesn't seem to support either) – ilkkachu Aug 31 '21 at 12:55
  • @blackbrandt, note that the question was tagged with [tag:zsh], while shopt -s extglob is a Bash feature. The zsh equivalent to f!(a)x would be f(^a)x (I think), with setopt extendedglob. Zsh would support the former too with setopt kshglob. Though note that that would also match things like fx and foox. OTOH, f[\!a]x seems to work in zsh, but not in Bash. – ilkkachu Aug 31 '21 at 12:58
  • 1
    @ilkkachu I thought it was more common, but indeed it isn't. POSIX requires ! to work and leaves ^ unspecified. – Gilles 'SO- stop being evil' Aug 31 '21 at 14:25