Master Linux Shell Wildcards: How * and ? Really Work
This article explains Linux shell wildcard behavior, demonstrates common patterns with practical examples, clarifies the difference between wildcards, meta characters, and escape sequences, and outlines the shell's parsing steps, helping readers avoid common pitfalls when using * , ?, [] and {} in commands.
Linux Shell Wildcards (Wildcard)
Wildcards are processed by the shell, not by the individual commands. When a wildcard appears in a command’s arguments, the shell expands it to matching file names on the filesystem; if no match is found, the pattern is passed unchanged to the command.
Example:
[chengmo@localhost ~/shell]$ ls a.txt b.txt c.oldIn the second command, *.txt matches a.txt and b.txt, so the command actually executed is ls a.txt b.txt. In the third command, d*.txt finds no matches, so the literal string d*.txt is passed to ls, resulting in an “cannot access” error.
Common Shell Wildcards
The most frequently used wildcards are *, ?, [], and {}. They resemble regular expressions but operate differently and should not be confused.
Shell Meta Characters
Meta characters are special symbols that the shell interprets before passing arguments to commands. They include operators such as |, &, ;, ( ), < >, and whitespace. These characters control command flow rather than file matching.
Shell Escape Characters
To treat a wildcard or meta character as a literal, you can use one of three quoting mechanisms: the escape character ( \), single quotes, or double quotes.
Example with escaping: [chengmo@localhost ~/shell]$ ls '*'.txt The asterisk loses its wildcard meaning and is treated as a literal character.
Shell Command Parsing Process
The shell processes a command in several steps: tokenization, expansion (parameter, command, arithmetic), and finally execution. Quoting influences which steps are performed. Double quotes allow parameter, command, and arithmetic expansion; single quotes skip all expansions.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
