Regular Expressions   «Prev  Next»
Lesson 3 Using quotes with regular expressions
Objective Identify how quotes affect regular expressions.

Using Quotes with Regular Expressions

How do quotes affect regular expressions in Unix?
In Unix, quotes play a significant role in handling regular expressions by allowing users to define and manipulate patterns in a more precise and controlled manner. Regular expressions are a powerful tool used for pattern matching, searching, and substitution within strings. To understand how quotes affect regular expressions in Unix, we will examine the two main types of quotes: single quotes (' ') and double quotes (" ").
  1. Single Quotes (' '):
    Single quotes (' ') are used to define a literal string, preserving the exact characters enclosed within them. They prevent variable expansion, command substitution, and interpretation of special characters, such as the wildcard or the backslash.
    In the context of regular expressions, single quotes are useful when you want to match a pattern that includes characters that have special meanings in Unix. For example, consider the following regular expression pattern:
    '\*\+'
    

    Here, the single quotes ensure that the asterisk (*) and the plus sign (+) are treated as literal characters, rather than their special meanings in regular expressions (zero or more occurrences, and one or more occurrences, respectively).
  2. Double Quotes (" "):
    Double quotes (" ") are used to define a string while allowing variable expansion and command substitution. However, they do not prevent the interpretation of special characters within the string.
    When working with regular expressions in Unix, using double quotes can lead to unexpected results if the string contains characters that have special meanings. For instance, consider the following regular expression pattern:
    "\*+"
    

    In this case, the asterisk (*) and the plus sign (+) will be interpreted as special characters, which could lead to undesirable pattern matching results. To avoid such issues, it is advisable to use single quotes when working with regular expressions that contain special characters.

The use of quotes in regular expressions within Unix is crucial for controlling the interpretation of patterns. Single quotes (' ') ensure that special characters are treated as literals, while double quotes (" ") allow for variable expansion and command substitution. When working with regular expressions, it is generally recommended to use single quotes to avoid unexpected behavior.

When you use grep, you should enclose the regular expression argument in quotes.
You already know that the shell interprets arguments before passing them to a command for execution. When using grep, the regular expression is always the first argument.
All remaining arguments are considered to be file names. For example, suppose you are searching the factsheet file for the pattern barbershop style.
The following SlideShow compares the results depending on the use of quotes.

1) With quotes, the shell processes the first two words
With quotes, the shell processes the first two words "barbershop style" factsheet
in the barbershop style for
in the barbershop style
lab1%

2) Without quotes, the shell sees each word as an argument.
Without quotes, the shell sees each word as an argument. The command searches for a single word, barbershop, in two files named style and factsheet. In this case, grep reports an error because it cannot find a file named style, but the grep searches factsheet and displays additional matches.


Enclosing Arguments in Quotes

Multi-word Argument

By placing quotes around a multi-word argument, you prevent the shell from using spaces as argument separators. Similarly, you must quote a regular expression if it contains metacharacters because they also have special meaning to the shell.
In fact, because almost every symbolic character means something to the shell, it is best to get into the habit of enclosing all regular expressions in quotes. Although you do not need quotes around a regular expression that does not contain spaces or symbolic characters, it is safer to use quotes all the time.
In most cases, either double quotes or single quotes will work, as long as they are the same on both sides of the argument.
In the next lesson, you will learn how to use the dot (.), asterisk (*), and brackets ([ ]) to match occurrences of a pattern.