In most shells nullglob isn't the default. That means, for example, if you run this command

ls *

in an empty directory, it will expand the * glob to a literal *, instead to an empty list of arguments. There are ways to change that behaviour, so that * in an empty directory will return an empty list of arguments, which would seem more intuitive.

So, is there a reason why nullglob is disabled by default? If so, what is that reason?

  • 32
    Somebody once made a bad choice that turned into "we've always done it this way". A very ubiquitous phenomenon (not only) in the software world. May 21, 2015 at 12:27
  • 4
    The original reason is that the nullglob option didn't exist back then. So to maintain backwards compatibility it must be disabled by default.
    – PM 2Ring
    May 21, 2015 at 12:48
  • 3
    While it does not specifically mention the null glob, some history on globbing is provided by this answer: unix.stackexchange.com/a/136409/22724
    – StrongBad
    May 21, 2015 at 13:10
  • 4
    I get the impression some think it should be obvious that nullglob should be enabled by default. I don't think it's obvious. That expansions happen in the shell before command invocation means that expanding to nothing is a less intuitive behavior than the glob remaining unchanged.
    – kojiro
    May 21, 2015 at 16:32
  • 4
    @kojiro Less intuitive to whom? Anyone with any familiarity with *NIX shells knows that * is a glob and expands to all existing files; how is it "intuitive" for there to be a special case where empty directory globs are "expanded" to a literal *? Jul 5, 2016 at 15:53

1 Answer 1


The nullglob option¹ would not be ideal in a number of cases. And ls is a good example:

ls *.txt

Or its more correct equivalent:

ls -- *.txt

(to list the contents of the non-hidden directories whose name ends in .txt, or the non-directory files with the same name patterns)

With nullglob on would run ls with no argument which is treated as ls -- . (list the current directory) if no files match, which is probably worse than calling ls with a literal *.txt as argument².

You'd have similar problems with most text utilities:

grep foo *.txt

Would look for foo on stdin if there's no txt file.

A more sensible default, and the one of csh, tcsh, zsh or fish 2.3+ (and of early Unix shells) is to cancel the command altogether if the glob doesn't match.

bash (since version 3) has a failglob option for that (interesting to this discussion, since contrary to ash, AT&T ksh or zsh, bash doesn't support local scopes for options³, that option when enabled globally does break a few things like the bash-completion functions).

Note that csh and tcsh are slightly different from zsh, fish or bash -O failglob in cases like:

ls -- *.txt *.html

Where you need all the globs to not-match for the command to be cancelled. For instance, if there's one txt file and no html file, that becomes:

ls -- file.txt

You can get that behaviour with zsh with set -o cshnullglob though a more sensible way to do it in zsh would be to use a glob like:

ls -- *.(txt|html)

In zsh and ksh93, you can also apply nullglob on a per-glob basis, which is a lot saner approach than modifying a global setting:

files=(*.txt(N))  # zsh
files=(~(N)*.txt) # ksh93

would create an empty array if there's no txt file instead of failing the command with an error (or making it an array with one *.txt literal argument with other shells).

Versions of fish prior to 2.3 would work like bash -O nullglob but give a warning when interactive when a glob has no match. Since 2.3, it works like zsh except for globs used in for, set or count.

Now, on the history note, the behaviour was actually broken by the Bourne shell. In prior versions of Unix, globbing was done via the /etc/glob helper and that helper behaved like csh: it would fail the command if none of the globs matched any file and remove the globs with no match otherwise.

So the situation we're in today is due to a bad decision made in the Bourne shell.

Note that the Bourne shell (and the C shell) came with another new Unix feature: the environment. That meant variable expansion (it's predecessor only had the $1, $2... positional parameters). The Bourne shell also introduced command substitution.

Another poor design decision of the Bourne shell was to perform globbing (and splitting) upon the expansion of variables and command substitution (possibly for backward compatibility with the Thompson shell where echo $1 would still invoke /etc/glob if $1 contained wildcards (it was more like pre-processor macro expansion there, as in the expanded value was parsed again as shell code)).

Failing globs that don't match would mean for instance that:

grep $pattern file

would fail the command (unless there are some a.whateverb files in the current directory). csh (which also performs globbing upon variable expansion) does fail the command in that case (and I'd argue it's better than leaving a dormant bug there, even if it's not as good as not doing globbing at all like in rc/ zsh / fish...).

¹ added in 2.0 in 1996 with the introduction of the shopt builtin, named after zsh's equivalent option, though bash had the allow_null_glob_expansion variable for that in earlier versions

² for which ls would likely report an error that the *.txt file doesn't exist, unless it has been created in the interval, or the current directory happens to be searchable but not readable and that file or directory exists. Try after mkdir -p '*.txt/wtf'; chmod a=,u=wx . for instance

³ version 4.4 saw some improvement on that front in that options set by set -o could be made local to functions with local - like in the Almquist shell, but that doesn't work for bash's second set of options, the ones set with shopt

  • There's another usability issue: nullglob appears to break tab-completion (pressing the tab key does nothing when it's enabled). Jul 5, 2016 at 16:01
  • 1
    It's possible to use nullglob on a per glob basis with bash - although the syntax is not as elegant as zsh files=$(shopt -s nullglob;echo *.txt)
    – Jon Nalley
    Sep 21, 2017 at 21:33
  • 7
    @JonNalley, that stores the concatenation (with space) of the file names (with possible transformation with xpg_echo) into a scalar variables. You'd need something like readarray -td '' files < <(shopt -s nullglob; printf '%s\0' *.txt) with bash 4.4 or above or (shopt -s nullglob; printf '%s\0' *.txt) | xargs -r0 cmd with GNU xargs for that to be usable at all with arbitrary file names. Or, still with bash4.4, use a helper function that uses local - (copied from ash 25 years later) for a local scope for options. Sep 22, 2017 at 7:31
  • These docs are very useful for precise definitions of what Bash does when expanding globs, including while nullglob and failglob are set/unset. May 6, 2022 at 20:44

You must log in to answer this question.

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