Unlike on other platforms you may have seen, on UNIX®, the shell is responsible for interpreting and expanding globs ("filename wildcards"). A called program will never see the glob itself; it will only see the expanded filenames as its arguments (here, all filenames matching *.log
):
grep "changes:" *.log
The base syntax for the pathname expansion is the pattern matching syntax. The pattern you describe is matched against all existing filenames and the matching ones are substituted. Since this substitution happens after word splitting, all resulting filenames are literal and treated as separate words, no matter how many spaces or other IFS
-characters they contain.
/
) always needs to be matched explicitly/
in the glob) nullglob
is set, non-matching globs are removed, rather than preserved failglob
is set, non-matching globs produce an error message and the current command is not executed nocaseglob
is set, the match is performed case-insensitive dotglob
is set, wildcard-characters can match a dot at the beginning of a filename dirspell
is set, Bash performs spelling corrections when matching directory names globstar
is set, the glob **
will recursively match all files and directories. This glob isn't "configurable", i.e. you can't do something like **.c
to recursively get all *.c
filenames. globasciiranges
is set, the bracket-range globs (e.g. [A-Z]
) use C locale order rather than the configured locale's order (i.e. ABC…abc…
instead of e.g. AaBbCc…
) - since 4.3-alphaNormally, when no glob specified matches an existing filename, no pathname expansion is performed, and the globs are not removed:
$ echo "Textfiles here:" *.txt Textfiles here: *.txtIn this example, no files matched the pattern, so the glob was left intact (a literal asterisk, followed by dot-txt).
This can be very annoying, for example when you drive a for-loop using the pathname expansion:
for filename in *.txt; do echo "=== BEGIN: $filename ===" cat "$filename" echo "=== END: $filename ===" doneWhen no file name matches the glob, the loop will not only output stupid text ("
BEGIN: *.txt
"), but also will make the cat
-command fail with an error, since no file named *.txt
exists.
Now, when the shell option nullglob
is set, Bash will remove the entire glob from the command line. In case of the for-loop here, not even one iteration will be done. It just won't run.
So in our first example:
$ shopt -s nullglob $ echo "Textfiles here:" *.txt Textfiles here:
and the glob is gone.
For example, to match something beginning with either 'S' or 'K' followed by two numbers, followed by at least 3 more characters:
[SK][0-9][0-9]???*