-->
Previous Table of Contents Next


Be careful in the use of apostrophes, or single quotes (‘), and backquotes (`, also known as grave accents). Table 18.6 lists what each mark does.

Table 18.6 Quotation Marks and Apostrophes

Symbol Meaning

Quotation marks disable filename generation and suppress parameter expansion; however, shell-variable and command substitution still take place.
The apostrophe disables all parsing. Whatever is enclosed within the apostrophes is passed on as a single parameter.
` The backquote, or grave accent, implies command substitution. Whatever is enclosed within backquotes is executed as though the command was performed on a line by itself. Any output placed on the standard output then replaces the command. The command line is then parsed again for parameters.

Consider the following command line:


echo Today\’s date and time are ‘date‘

It produces this output:


Today’s date and time are Mon May 18 14:35:09 EST 1994

To make the echo command behave properly, the ’s in Today’s in the preceding command was preceded by a backslash (\), also called the escape character(Today\’s). Virtually every non-alphanumeric character on your keyboard has some special meaning to the shell. To use escape characters any of the special characters in a string and to prevent the shell from interpreting the character, you must “escape” the character—that is, you must precede it with the backslash. If you want to pass the backslash character itself, use \\. To pass a dollar sign to a command, use \$.

Regular Expressions

A regular expression is a series of standard characters and special operators. Regular expressions are useful for searching for a string of characters in a file. Regular expressions are often used with the grep family of tools: grep, egrep, and fgrep, but are also used with other UNIX commands.

The simplest kind of regular expression is a string. A string is a set of characters, such as and. The syntax for a grep command is as follows:


grep  string  filename

For example, to search for the word hand in a specific file named michael, you would enter this command:


grep  hand  michael.txt

which might return the result


on the other hand, michael has been working hard this past

if that was the only line of the text file that contained the word hand. Grep will return every line of a text file that has a match to the string.

Regular expressions use special characters. The special characters used with regular expressions are the period (.), asterisk (*), square brackets ([]), backslash (/), caret (^), and dollar sign ($). Table 18.7 summarizes these special characters and their behavior in regular expressions.

Table 18.7 Special Characters in Regular Expressions

Character Description

. Matches a single character, unless the single character is a line-return. For example, b.d will match bad and bod.
* Matches zero or more of the preceding regular expression. So the pattern 4* matches no 4s, 1 4, 2 4s, and so on.
[] Used to group a set of multiples for matches. Remember that unlike DOS, UNIX is case-sensitive. So to search for all instances of the name Michael, you could use [Mm]ichael to search for both michael and Michael, but not MICHAEL. If you want to search for an actual] character, either you can use []], or you can use the backslash as an escape character to treat the right bracket as a text character, like so: /]. A dash inside brackets acts as a range, so [a–j] is the same as [abcdefghij].
/ Used to escape the special behavior of these special characters and treat them as text to be searched for in a string. So * matches everything, but /* matches only a line with the * character in it. // is used to search for a backslash character, of course.
^ If ^ is at the beginning of the string, it matches a line only if the string is at the beginning of the line. So if you have a text file of telephone numbers sorted by area code, the regular expression ^704 will match all telephone numbers with the area code 704, but not a telephone number 407-555-7043.
$ If a $ is the last character in a regular expression,it matches the expression to a line in the file if the expression is at the end of the file.

You can define how many of a given character to match by using the curly bracket pair {}. For example, the command


g\{3,4}

matches any line in the text file that contains either ggg or gggg.

If you maintain a large file of older mail, the command


grep  ‘whatever’  ~/mail/*

will search for the string whatever in the mail directory. This is useful if you recall that your recent acquaintance Dave Quigman included his telephone number in his sigfile, but you can’t remember what folder you saved his message into. This command


grep  “Quigman”  ~/mail/*

will match all instances of the name.

However, a more efficient way may be to match the telephone number itself. Let’s say his telephone number is in the area code 408. So the command


grep   ‘408.[0-9]\{3\}.[0-9]\{4\}’   ~/mail/*

finds all telephone numbers that start with 408. Notice the periods separating the 408, the [0–9]\{3\} and [0–9]\{4\}. The period matches any single character, which allows you to match the telephone number 408-555-1212 or 408.555.1212, because some people use periods to separate the telephone numbers.


Previous Table of Contents Next