| 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.
|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.
An anchor is a metacharacter such as ^ or $ that restricts a match to a particular position.
A shell variable is a place to store data that is used by the shell.