Regular Expressions/Shell Regular Expressions

The Unix shell recognises a glob syntax for use with filename substitution. With the extglob extension of ksh and bash, it is equivalent to a basic form of regular expression in expressiveness.

Operators
Operator Effect
? The hook operator specifies any single character.
[ ] boxes enable a single character to be matched against a character lists or character range.
[! ] A compliment box enables a single character not within in a character list or character range to be matched. A non-standard equivalent, supported by GNU bash, is the caret syntax [^ ].
* An asterisk specifies zero or more characters to match.
?(pattern-list) Matches zero or one occurrence of the given patterns. extglob extension.
*(pattern-list) Matches zero or more occurrences of the given patterns. extglob extension.
+(pattern-list) Matches one or more occurrences of the given patterns. extglob extension.
@(pattern-list) Matches exactly one of the given patterns. extglob extension.
!(pattern-list) Matches anything except one of the given patterns. extglob extension.

Differences from common regular expressions are:

  • The asterisk and hook operators mean different things.
  • The compliment bow is formed using the exclamation mark (!) in the POSIX standard, not a caret (^). Some shells, such as GNU bash, allow the caret to be used as an extension.
  • The extglob extensions are not standard. They are only available in Korn shell (enabled by default) and GNU bash (enabled via shopt -s extglob).

Use in Tools

edit

Tools and languages that utilize this regular expression syntax include:

  • For the general glob syntax, all POSIX "bourne-compatible" shells.
  • For the extglob part, Korn shell and bash.
edit