Bash Patterns

From NovaOrdis Knowledge Base
Jump to: navigation, search




Bash patterns are a simple regular expression language defined by the metacharacters and rules described below.

Variable expansion can be used inside patterns:

echo ${a#${b}}

prints "thing".


The following characters have a special meaning when a bash pattern is evaluated, and they need to be escaped to be matched literally:


* (star) matches any string, including the null string. When the globstar shell option is enabled, and ‘*’ is used in a filename expansion context, two adjacent ‘*’s used as a single pattern will match all files and zero or more directories and subdirectories. If followed by a ‘/’, two adjacent ‘*’s will match only directories and subdirectories.


? (question mark) matches a single character. The dot ('.') is not a metacharacter.


Matches any one of the enclosed characters.

For example:

[- :]

matches '-', space and ':'.

A pair of characters separated by a hyphen denotes a range expression; any character that falls between those two characters, inclusive, using the current locale’s collating sequence and character set, is matched. A digit is matches as follows:


A ‘-’ may be matched by including it as the first or last character in the set.

A ‘]’ may be matched by including it as the first character in the set.


If the first character following the ‘[’ is a ‘!’ or a ‘^’ then any character not enclosed is matched. The match is for one character only.

To match a known number of characters, repeat the [!...] expression the known number of times. [!...]* does not work to match an undefined number of not-enclosed characters; it will match just one non-enclosed character then all the characters. This behavior is different than sed behavior on negation.


Within ‘[’ and ‘]’, character classes can be specified using the syntax [:class:], where class is one of the following classes defined in the POSIX standard:

  • alnum
  • alpha
  • ascii
  • blank
  • cntrl
  • digit
  • graph
  • lower
  • print
  • punct
  • space
  • upper
  • word: matches letters, digits, and the character ‘_’.
  • xdigit

A character class matches any character belonging to that class.


To match a '/' (slash), use '\/'. If used in replacement constructs as variable expansion replacement, there is no need to escape slashes in the replacement string.


~ (tilda)


' (single quote) must be escaped to match:



" (double quote) must be escaped to match:



(semicolon) must be escaped to match


Parentheses must be escaped to match


Non-Special Characters

These characters do not need to be escaped in bash patterns to match:

\ # forward slash
. # dot - does NOT match any character, it just matches a dot
: # column


See / above.

Pattern Lists


Matches zero or one occurrence of the given patterns.


Matches zero or more occurrences of the given patterns.


Matches one or more occurrences of the given patterns.


Matches one of the given patterns.


Matches anything except one of the given patterns.