One common task in shell scripts is working on text patterns. The easiest way to refer to a specific text pattern is by entering it literally, such as in the command grep linux *, where all files in the current directory are searched for the literal text linux. This, however, becomes somewhat more complicated if you don't know how a word is written. Many commands allow you to work with regular expressions to solve that problem. Some of the most common commands that can work with regular expressions are grep, sed, and awk.
An example of a command that uses a regular expression is grep 'lin.x' *. In this command, the pattern 'lin.x' is the regular expression. In it, the dot refers to any character. Also notice that the complete regular expression appears between single quotes; this makes sure the shell will not interpret it. I recommend always using quotes in a regular expression; this adds more clarity to what exactly should happen. Table 27-1 gives an overview of some of the most common special characters that you can use in regular expressions.
Table 27-1. Most Common Special Characters in Regular Expressions
A Refers to the beginning of a line. For example, grep -ls 'Ahosts' * shows only files where the text hosts is in the beginning of the line.
$ Refers to the end of a line. For example, grep -ls 'false$' * shows only files that have the text false in the end of a line.
. Refers to any character, with the exception of the newline character. An example of its use is the command grp -ls 'h.st' *.
[xy] Refers to either x or y. For example, grep 'li[vf]e' * would find either live or love.
[Ax] Ignores the text between brackets.
Was this article helpful?