DistributedNetworks DistributedNetworks

Regular Expressions   «Prev  Next»
Lesson 5 Matching the position of a pattern
Objective Create regular expressions using the ^ and $ metacharacters.

Matching the Position of a Pattern

By default, regular expressions can match a pattern anywhere within a line, but you can refine this behavior. To match patterns only at the beginning of a line, place a caret (^) before the regular expression.
To match patterns only at the end of a line, place a dollar sign ($) after the regular expression.
Here are some examples.

Command Description
grep ^stat file Matches all lines that begin with patterns like static or stately.
grep stat$ file Matches all lines that end with patterns like rheostat.

The ^ and $ metacharacters are sometimes called anchors[1]. In the examples above, note that the expressions do not require quotes because they do not contain characters, such as spaces, *, or [ ], that would be interpreted by the shell. By themselves, the ^ and $ do not cause problems for the shell. However, here is a situation where quotes are needed because of the brackets:
% grep "^[Ss]tat" file

This command displays lines that begin with the patterns Stat or stat. But watch what happens using a similar combination with the $metacharacter. The following examples seeks to match ball or bell at the end of a line, but instead the shell displays an error message:
% grep "b[ae]ll$" file

Variable syntax

When the shell sees the $ inside double quotes, the shell assumes it’s working with a shell variable[2].
To make the $ act like an anchor metacharacter, you should use single quotes:
% grep 'b[ae]ll$' file 
When working with regular expressions on the command line, it is best to use single quotes. That way, you do not need to remember whether to use single quotes, double quotes, or no quotes.
In the next lesson, you will learn how to use a backslash (\) to disable the special meaning of a character.
[1]anchor: An anchor is a metacharacter such as ^ or $ that restricts a match to a particular position.
[2]shell variable: A shell variable is a place to store data that is used by the shell.