Master Linux Shell Wildcards: How Patterns, Metacharacters, and Escapes Work
This article explains Linux shell wildcard behavior, common wildcard symbols, meta characters, escape mechanisms, and the shell's parsing process, illustrating each concept with practical command examples and diagrams to help readers understand how the shell expands patterns and handles special characters.
1. Linux Shell Wildcards (Wildcard)
Wildcards are processed by the shell, not by the individual commands. They appear only in command arguments and are expanded by the shell into matching file names or paths. If matches exist, the shell replaces the pattern with the list of files; otherwise the pattern is passed unchanged to the command.
Example: ls a.txt b.txt c.old In the second command, *.txt matches a.txt and b.txt, so the command 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.
2. Common Shell Wildcards
The typical wildcard symbols are *, ?, [], and {}. They resemble regular expressions but are not interchangeable; they are simple pattern-matching tokens used by the shell.
3. Shell Metacharacters (Special Characters)
Metacharacters are interpreted by the shell before any command runs. They include characters such as |, &, ;, ( ), < >, and whitespace. They are used for command separation, pipelines, background execution, and other control functions, distinct from wildcard usage.
Excerpt from man bash :
metacharacter: A character that, when unquoted, separates words. One of the following: | & ; ( ) < > space tab control operator: A token that performs a control function. One of the following symbols: || & && ; ;; ( ) | <newline>
4. Shell Escape Characters
To treat a wildcard or metacharacter as a literal character, the shell provides three quoting mechanisms: the escape character ( \), single quotes, and double quotes. Using an escape removes the special meaning of the character.
Example: ls \*.txt Here the asterisk is escaped, so the pattern is not expanded and ls receives the literal string *.txt, which typically results in "cannot access" if no such file exists.
5. How the Shell Parses a Script
The shell processes a command line in several steps: lexical analysis, expansion (including wildcard expansion), redirection handling, and finally execution. Quoting affects which steps are performed. Double quotes skip only certain expansions (parameter, command, arithmetic), while single quotes skip all expansions, passing the text directly to the command.
Understanding these stages helps explain why patterns behave differently under various quoting contexts.
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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
