Expanding to nothing

You may also have noticed with some experimentation with globs that if the pattern does not match anything, it expands to itself, unchanged:

$ printf '%s\n' c*
c*

Because none of our test files start with c, the glob is unexpanded and unchanged. This may seem strange; surely it would be better if the pattern expanded to nothing at all? It makes more sense when you consider the behavior of many classic Unix commands; if there are no arguments, they default to assuming input is coming from standard input. For example:

$ cat -- c*
cat: 'c*': No such file or directory

Because the pattern here did not expand, cat saw the c* argument, found the file named c* did not exist, and reported that to us, with an error message that gives us some idea of what went wrong.

Bash does allow us to alter this behavior; if we set the nullglob option, c* really will expand to nothing at all:

bash$ shopt -s nullglob
bash$ printf '%s\n' c*
bash$

However, note that if we then used the c* pattern on a cat command line, for example, and there were no matching files, it would simply stop, waiting for input from the terminal  probably not what you intended to happen:

bash$ shopt -s nullglob
bash$ cat -- c*

So nullglob can be a very useful option  but be careful when using it in scripts.