Shell, explained
What does * mean in the terminal? Globbing explained
echo *.txt
* is a wildcard that the shell expands before the command runs: the pattern is replaced by every file name in the current folder that matches it. The command never sees the *, only the list of names.
*.txt becomes the matching names
echo never sees the pattern. It is handed three file names and prints them.
Tap a word to follow it
echo receives 3 arguments
When nothing matches
In bash a pattern that matches nothing is passed on as is, so ls looks for a file literally named *.xyz.
Tap a word to follow it
Nothing matched, so the pattern stays. ls replies: ls: *.xyz: No such file or directory
ls receives 1 argument
Quotes switch it off
Inside quotes * is just a character: echo prints *.txt.
Tap a word to follow it
echo receives 1 argument
Part by part
echo- Prints its arguments, separated by spaces.
*- Matches any run of characters in a file name, including none.
.txt- Must match literally: the name has to end in .txt.
Common mistakes
rm * .txt
One stray space turns this into rm * and rm .txt: it deletes every file in the folder. Run echo with the same pattern first to see what it matches.
Expecting * to match hidden files
* skips names that start with a dot, like .env. Match them with .* or turn on shopt -s dotglob in bash.
Try it
$ echo *Every visible name in the folder.
$ echo report?.txt? matches exactly one character.
$ echo [nt]*.txt[nt] matches one of the listed characters.
Questions
Why does zsh say “no matches found”?
zsh treats a pattern that matches nothing as an error instead of passing it on. Quote the pattern (find . -name '*.txt') when you want the program to see the *.
Is * the same as a regular expression?
No. In a glob, * means any characters; in a regular expression (grep, sed), * means “repeat the previous character”. .* is the regex way to say any characters.
Does * search subfolders?
No, only the current folder. In bash, shopt -s globstar enables **/*.txt for all subfolders, or use find.
Terminaster
Ask about any command and get a picture like this one.
The Terminaster tutor draws these visuals for your own questions, then turns what you learned into spaced-repetition cards so it sticks.
The ideas behind it
Filename expansion (globbing)
Filename expansion is the shell step that replaces unquoted pathname patterns such as *, ?, and bracket expressions with matching existing pathnames before launching a command.
Quoting and escaping
Shell quoting is syntax the shell consumes while building command arguments. Single quotes preserve every enclosed character literally; double quotes still allow parameter, command, and arithmetic expansion while suppressing word splitting and pathname expansion; an unquoted backslash protects the next character, except that backslash-newline continues the line.