$ 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?
$ 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?
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
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
^
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
[^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
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
!
to work and leaves ^
unspecified.
– Gilles 'SO- stop being evil'
Aug 31 '21 at 14:25
setopt nobanghist
in everybody's default~/.zshrc
would make this world a happier place. – Philippos Aug 30 '21 at 13:37