Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


Hour 9
Quoting

By Frank Watson

In the preceding chapter, you looked at shell substitution, which occurs automatically whenever you enter a command containing a wildcard character or a $ parameter. The way the shell interprets these and other special characters is generally useful, but sometimes it is necessary to turn off shell substitution and let each character stand for itself. Turning off the special meaning of a character is called quoting, and it can be done three ways:

  Using the backslash (\)
  Using the single quote ()
  Using the double quote ()

Quoting can be a very complex issue, even for experienced UNIX programmers. In this chapter you look at each of these forms of quoting and how to use them. You learn a series of simple rules to help you understand when quoting is needed and how to do it correctly.

Quoting with Backslashes

First, use the echo command to see what a special character is. The echo command is covered in more detail in Chapter 13, “Input/Output,” but it is a simple command that just displays the arguments it has been given on the command line. For example,

echo Hello world

displays the following message on your screen:

Hello world

Here is a list of most of the shell special characters (also called metacharacters):

* ? [ ] ' " \ $ ; & ( ) | ^ < > new-line space tab

Watch what happens if you add one of them to the echo command:

echo Hello; world

It now gives this error result:

Hello
sh: world: Command not found

The semicolon (;) character tells the shell that it has reached the end of one command and what follows is a new command. This character enables multiple commands on one line. Because world is not a valid command, you get the error shown.

You can resolve the problem by putting a backslash (\)in front of the ; character to take away its special meaning, enabling you to display it as a literal character:

echo Hello\; world

The backslash causes the ; character to be handled as any other normal character. The resulting output is

Hello; world

To display any shell special character reliably from echo, you must escape it, that is, precede it by a backslash. Using the backslash quotes the character that follows it, using it as a literal character. Each of the special shell characters previously listed causes a different problem symptom if you try to echo it without quoting it. This need to quote special characters occurs in many other UNIX commands as you see later.

Notice in the previous example that the quoting character, the backslash, is not displayed in the output. The shell preprocesses the command line, performing variable substitution, command substitution, and filename substitution, unless the special character that would normally invoke substitution is quoted. The quoting character is then removed from the command arguments, so the command being run never sees the quoting character. Here is a different example where quoting is needed:

echo You owe $1250

This seems like a simple echo statement, but notice that the output is not what you wanted because $1 is a special shell variable:

You owe 250

The $ sign is one of the metacharacters, so it must be quoted to avoid special handling by the shell:

echo You owe \$1250

Now you get the desired output:

You owe $1250

Notice the \ quoting character is not present in the output.

Using Single Quotes

Here is an echo command that must be modified because it contains many special shell characters:

echo <-$1250.**>; (update?) [y|n]

Putting a backslash in front of each special character is tedious and makes the line difficult to read:

echo \<-\$1250.\*\*\>\; \(update\?\) \[y\|n\]

There is an easy way to quote a large group of characters. Put a single quote () at the beginning and at the end of the string:

echo '<-$1250.**>; (update?) [y|n]'

Any characters within single quotes are quoted just as if a backslash is in front of each character. So now this echo command displays properly.


Note:  
Quoting regular characters is harmless. In the previous example, you put single quotes around a whole string, quoting both the special characters and the regular letters and digits that need no quoting. It does not hurt to quote regular characters because quoting takes away any special meaning from a character and does not mind if that character had no special meaning to begin with. This is true for the backslash, single quotes, and double quotes.

If a single quote appears within a string to be output, you should not put the whole string within single quotes:

echo 'It's Friday'

This fails and only outputs the following character, while the cursor waits for more input:

> _

The >sign is the secondary shell prompt (as stored in the PS2 shell variable), and it indicates that you have entered a multiple-line command—what you have typed so far is incomplete. Single quotes must be entered in pairs, and their effect is to quote all characters that occur between them. In case you are wondering, you cannot get around this by putting a backslash before an embedded single quote.

You can correct the previous example by not using single quotes as the method of quoting. Use one of the other quoting characters, such as the backslash:

echo It\'s Friday


Previous Table of Contents Next