Skip to main content

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.

A negated character class in Bash is a pattern matching construct that evaluates to exactly one character not present in a specified set or range. It is evaluated by the Bash pattern matching engine during pathname expansion (globbing), case statement execution, conditional expression matching ([[ string == pattern ]]), and parameter expansion (e.g., ${var#pattern}, ${var%pattern}, ${var//[!0-9]/}).
[!CHARACTERS]
[^CHARACTERS]

Technical Mechanics

  • Negation Operators: The exclamation mark (!) is the POSIX-compliant negation operator for shell pattern matching. Bash also accepts the caret (^) to mirror regular expression syntax, but ! is required for strict POSIX portability.
  • Positional Requirement: The negation operator (! or ^) must be the immediate first character following the opening bracket [. If placed anywhere else within the brackets, it loses its negation property and is parsed as a literal character.
  • Arity: A negated character class always consumes exactly one character in the target string. It cannot match zero characters, nor can it match multiple characters.

Syntax Variations

Discrete Sets Matches any single character that is not explicitly listed.
[!abc]    # Matches any character except 'a', 'b', or 'c'
Character Ranges Matches any single character whose collation value falls outside the defined start and end boundaries. Bash evaluates character ranges using the current locale’s collation sequence (determined by the LC_COLLATE environment variable). In many common locales (such as en_US.UTF-8), this utilizes dictionary sorting (e.g., aAbBcC), meaning a negated range like [!A-Z] may unexpectedly exclude lowercase letters. Strict ASCII/Unicode byte-order evaluation is only applied if the locale is explicitly set to C or POSIX.
[!0-9]    # Matches any character outside the range '0' through '9'
Combined Sets and Ranges Multiple ranges and discrete characters can be combined within a single negated class.
[!a-fA-F0-9_] # Matches any character that is not a hex digit or underscore

Parsing Rules for Special Characters

When constructing a negated character class, certain characters require specific positioning to be treated as literals rather than syntax operators:
  • Literal Hyphen (-): To exclude a literal hyphen without creating an invalid range, the hyphen must be placed immediately after the negation operator or at the very end of the class.
[!-abc]   # Excludes '-', 'a', 'b', and 'c'
[!abc-]   # Identical parsing to the above
  • Literal Closing Bracket (]): To exclude a literal closing bracket, it must be placed immediately after the negation operator. Bash parses the first ] it encounters as the end of the class unless it is in this specific position.
[!]]      # Excludes the literal character ']'
  • Literal Exclamation/Caret (! / ^): If these characters appear anywhere other than the first position, the parser treats them as standard literal characters to be included in the set.
[a!b]     # NOT NEGATED. Matches exactly 'a', '!', or 'b'
[!a!b]    # NEGATED. Excludes 'a', '!', and 'b'
Master Bash with Deep Grasping Methodology!Learn More