Expansion happens before the program starts
When you type ls *.txt, ls never sees the asterisk. Bash expands the pattern against the directory and replaces the word with the matching names, which are passed to ls as separate arguments. If nothing matches, bash passes the pattern through unchanged unless a shell option says otherwise. The program receives only words, and it cannot tell whether a word came from a glob.
This is why globs are safe for building argument lists. The names are passed as separate arguments with their spaces intact, provided that you quote the expansion. It is also why globs are the preferred way to process files in a script, instead of parsing the output of ls.
The basic glob characters
An asterisk matches any string, including the empty string, but not a leading dot. A question mark matches exactly one character. A bracket expression such as [abc] matches one character from the set, and [a-z] matches a range according to the collation of the locale. An exclamation mark inside the brackets, as in [!abc], negates the set. The leading dot rule is important: a hidden file is matched only by a pattern that begins with a dot, so the normal listing ignores files such as .profile.
cd /workspace
mkdir -p globs
touch globs/a.txt globs/b.txt globs/c.log globs/.hidden.txt
echo globs/*.txt
echo globs/?.logThe first command prints two names, and the hidden file is not among them. The second matches the single-character name c.log.
Brace expansion is textual
Braces are a different mechanism. Bash expands {a,b,c} into three words, and the result does not depend on the file system. The command echo {a,b}.txt prints a.txt and b.txt even if neither exists. Brace expansion is also how you write sequences, such as {1..5} or {a..e}, and it can nest, so a single command can build a tree of directory names.
cd /workspace
echo {a,b}.txt
mkdir -p backup/{2025,2026}/{jan,feb}
ls backup/2026Brace expansion happens first, which is why the command creates four directories in one step. Each combination becomes one path, and mkdir -p creates the missing parents.
The order of expansions
Bash performs several expansions in a fixed order: brace expansion, then tilde expansion, parameter and command substitution, arithmetic, word splitting, and pathname expansion, which is glob matching. Brace expansion happens before globbing, so echo g{a,b}* first becomes two patterns and then matches each. Word splitting happens before globbing as well, which is why an unquoted variable containing an asterisk is expanded into matches. Quoting a variable stops word splitting and globbing, and this is why quotes are essential around variables that hold names.
A glob that matches nothing is left as written by default. The command echo *.zip in an empty directory prints the literal string *.zip. Extended globs and nullglob, which the next unit covers, change this behavior.
Sorting and locale
Glob matches are sorted according to the collation of the current locale. In the C locale, uppercase letters sort before lowercase ones, and punctuation sorts by byte value. In a UTF-8 English locale, letters are compared more loosely, and the order of names that differ only in case or punctuation can change. A script that depends on order should set the locale or sort the output explicitly.
Why a glob is not a regular expression
Globs and regular expressions look similar, and they are different languages. A glob describes names and always matches a whole word, so the pattern *.txt means any name that ends in .txt. A regular expression describes text and can match anywhere in a line, so the same letters in grep mean something else. The question mark in a glob matches one character, while in a regular expression it makes the previous item optional. Mixing them is a frequent source of mistakes, especially in commands such as grep and find that take one or the other.
When you pass a glob to a command that expects a regular expression, the glob is expanded first by the shell. The command then receives the list of names, and the expression is applied to something it was not written for. Put patterns for grep in quotes so the shell does not expand them, and use find -name for glob matching on names.
Building paths with braces and variables
Brace expansion combines well with variables. The command mkdir -p "$base"/{src,test,docs} creates three directories under a base path, and the quotes keep the base intact when it contains spaces. Brace expansion runs before parameter expansion, so the list inside the braces is fixed text, and only the variable outside the braces is filled in later. Keep the list literal and the variable outside, as shown here, because a list stored in one string variable is a single word and is not split into the braces' items.
Sequences are handy for numbered files. The expansion file{1..3}.txt gives three names, and {01..10} keeps the leading zeros, which matters for sorting. A step can be given as in {0..10..2}, which counts by twos. These forms make test data easy to generate without a loop.
Globs in loops and conditions
A glob is most often used in a for loop, as in for f in *.log; do ...; done. The loop receives each matching name in turn, and the shell does the directory read for you. Quote the variable inside the loop, as in "$f", so that a name with spaces remains one argument. A loop over a glob is the clearest way to process a small, known set of files, and it avoids the parsing hazards of ls.
The test command can check a glob too, but only through a loop or an array, because a glob that matches several names gives a test several words. The usual check is to see whether a pattern matched anything, which the nullglob unit covers in detail. Until you have that option, test the first match with the -e test, which is false when the literal pattern was returned.
Practice in the lab
The lab creates a few files, matches them with a glob, and creates a set of directories with one brace expression. The checks compare the words the shell produced, so you can see exactly what the expansion gave.