Regular Expressions/Shell Regular Expressions

From Wikibooks, open books for an open world
Jump to navigation Jump to search

The Unix shell recognises a limited form of regular expressions used with filename substitution:

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.
* An asterisk specifies zero or more characters to match.
?(pattern-list) Matches zero or one occurrence of the given patterns.
*(pattern-list) Matches zero or more occurrences of the given patterns.
+(pattern-list) Matches one or more occurrences of the given patterns.
@(pattern-list) Matches exactly one of the given patterns.
!(pattern-list) Matches anything except one of the given patterns.

Note that the latter five constructs can only be used in bash and only if the extglob option has been enabled using the bash-builtin "shopt".

Some operators behave differently in the shell: The asterisk and hook operators do not not need to follow a previous character in the shell and they exhibit non traditional regular expression behaviour.

Unsupported Constructs: Within the shell, a compliment box is formed using the pling symbol. The shell does not support the use of a careted box for character list exclusion. In the shell, a caret symbol within a box will simply be treated as one of the characters within the character list for matching.

Use in Tools[edit | edit source]

Tools and languages that utilize this regular expression syntax include:

  • Bourne compatible shells

Links[edit | edit source]