I recetnly came across

setopt extended_glob

...in order to enable extended globbing which allows for a number of cool wildcard additions, like excluding specific patterns, for example:

ls ^foo*

...will use ls on every path in your current directory except for patterns that match foo*.

I found one tutorial suggesting to put setopt extended_glob inside your .zshrc, but I guess since many zsh config templates miss that option and the option being disabled by default it has some downsides or even side-effects?

Or is it absolutely harmless always enabling extended_glob via putting it inside one's .zshrc?


I would assume the potential issue is that ^ is quite a valid character in file names, so the meaning of a pattern containing it changes based on if the option is set or not:

$ touch 'foo' 'bar' '^foo' '^bar'
$ ls ^foo*
$ setopt extendedglob
$ ls ^foo*           
bar  ^bar  ^foo

A standard shell would take the ^ as an ordinary character, so the feature is probably disabled by default for compatibility.

As long as you remember what options are enabled, i.e. know how the shell interprets the globs, leaving extended_glob enabled shouldn't be a problem in interactive use.

For scripts that don't expect it but use weird filenames, it would be an issue, but non-interactive shells shouldn't read .zshrc, so setting it there should be ok. Just don't set it in .zshenv.

The ksh-style extended globs (@(...|...) etc., setopt kshglob) have a similar issue in that they conflict with how zsh handles parenthesis in globs. @(f|b) means different things depending on if kshglob is set or not.

| improve this answer | |
  • Note that ^ is special in several other shells (Bourne, rc, akanga, es, fish). Also in csh, tcsh, bash when the first character of a command line. a~b (also special with extendedglob) is not special in any though. – Stéphane Chazelas Mar 18 '18 at 21:58
  • 1
    ksh globs also conflict with Bourne/POSIX when used in expansions. For instance a='@(*)'; echo $a is meant to output the file names that start with @( and end with ) but outputs all non-hidden files instead bash -O extglob or zsh -o kshglob -o globsubst -o nobareglobquals. ksh does not recognise them when used in the result of expansions. – Stéphane Chazelas Mar 18 '18 at 22:06
  • ^ is also often used in git commands, such as git diff HEAD^ which makes the use of it in zsh quite problematic if you use git often. – Zaroth Jul 27 '19 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.