A single character pattern in Bash is a globbing metacharacter used in pathname expansion and string matching that evaluates to exactly one arbitrary character. The primary operator for this is the question mark (Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
?), while bracket expressions ([...]) provide constrained single-character matching.
The ? Metacharacter
The ? operator matches any single character in a string or filename. It requires exactly one character to be present; it evaluates strictly to a length of one and will not match zero characters or multiple characters.
- Path Boundaries: When used in pathname expansion,
?will not match a directory separator slash (/). - Hidden Files: By default,
?will not match a leading period (.) representing a hidden file or directory. The shell optionshopt -s dotglobmust be enabled to override this behavior. - Null Byte: It cannot match the null character (
\0), as Bash strings are null-terminated.
Bracket Expressions [...]
A bracket expression evaluates to exactly one character that exists within the enclosed set or range. Despite containing multiple characters inside the brackets, the entire construct represents only a single character position in the resulting match.
- Collation and Locale (
LC_COLLATE): In many default locales (e.g.,en_US.UTF-8), bracket ranges like[A-Z]or[a-z]use dictionary sorting (aAbBcCdD...). Consequently,[A-Z]will unexpectedly match lowercase letters (except ‘a’), and[a-z]will match uppercase letters (except ‘Z’). To enforce strict ASCII byte-value sorting, developers must setLC_COLLATE=Cor use POSIX character classes (e.g.,[[:upper:]]). - POSIX Character Classes: Supports classes like
[[:alpha:]]or[[:digit:]], which resolve to a single character match from within that class while remaining locale-aware and safe from range collation issues. - Negation: Prefixing the set with
!or^immediately after the opening bracket inverts the match, evaluating to exactly one character not present in the defined set.
], -, ^, !) can be safely escaped with a backslash (\) to be treated as literal characters.
- Literal
]: Placed as the first character in the set, or immediately following the negation operator ([]abc]or[!]]). - Literal
-: Placed as the first or last character in the set ([-abc]or[abc-]). - Literal
^or!: Placed anywhere except the first position ([ab!c^]).
Contextual Execution
Single character patterns are evaluated dynamically based on the syntax context:- Pathname Expansion (Globbing): Evaluated against the file system. If no file matches the pattern, Bash leaves the pattern as a literal string by default (modifiable via
shopt -s nullgloborfailglob). - Conditional Expressions (
[[ ... ]]): Evaluated as a pattern matching operator against strings on the right side of the==or!=operators. - Case Statements: Evaluated against the test string to determine execution branching.
Escaping and Literals
To suppress the special meaning of single character patterns and treat them as literal characters outside of bracket expressions, they must be quoted or escaped using a backslash (\), single quotes ('...'), or double quotes ("...").
Extended Globbing (extglob)
When the extglob shell option is enabled (shopt -s extglob), Bash alters the behavior of ? when combined with parentheses. This creates a composite pattern that matches zero or one occurrence of a specified pattern list, deviating from the strict “exactly one character” rule of the standalone ?.
Master Bash with Deep Grasping Methodology!Learn More





