-->
Previous Table of Contents Next


The Importance of Quotation Marks

The use of the different types of quotation marks is very important in shell programming. All three kinds of quotation marks and the backslash character are used by the shell to perform different functions. The double quotation marks (“”), the single quotation marks (’’), and the backslash (\) are all used to hide special characters from the shell. Each of these methods hides varying degrees of special characters from the shell.

The double quotation marks are the least powerful of the three methods. When you surround characters with double quotes, all the whitespace characters are hidden from the shell, but all other special characters are still interpreted by the shell. This type of quoting is most useful when you are assigning strings that contain more than one word to a variable. For example, if you want to assign the string hello there to the variable greeting, type the following command:


greeting=”hello there” (for bash and pdksh)

set greeting = “hello there” (for tcsh)

This command stores the hello there string into the greeting variable as one word. If you type this command without using the quotes, you do not get the results that you want. bash and pdksh will not understand the command and will return an error message. tcsh simply assigns the value hello to the greeting variable and ignores the rest of the command line.

Single quotes are the most powerful form of quoting. They hide all special characters from the shell. This is useful if the command that you enter is intended for a program other than the shell.

Because the single quotes are the most powerful, you can write the hello there variable assignment using single quotes. You may not always want to do this. If the string being assigned to the greeting variable contains another variable, you have to use the double quotes. For example, if you want to include the name of the user in your greeting, then type the following command:


greeting=”hello there $LOGNAME” (for bash and pdksh)

set greeting=”hello there $LOGNAME” (for tcsh)


Tip:  
Remember that the LOGNAME variable is a shell variable that contains the Linux username of the person who is logged in to the system.

This stores the value hello there root into the greeting variable if you are logged in to Linux as root. If you try to write this command using single quotes, it will not work because the single quotes hide the dollar sign from the shell and the shell doesn’t know that it is supposed to perform a variable substitution. The greeting variable will be assigned the value hello there $LOGNAME if you write the command using single quotes.

Using the backslash is the third way of hiding special characters from the shell. Like the single quotation mark method, the backslash hides all special characters from the shell, but it can hide only one character at a time, as opposed to groups of characters. You can rewrite the greeting example using the backslash instead of double quotation marks using the following command:


greeting=hello\ there (for bash and pdksh)

set greeting=hello\ there (for tcsh)

In this command, the backslash hides the space character from the shell, and the string hello there is assigned to the greeting variable.

Backslash quoting is used most often when you want to hide only a single character from the shell. This is usually done when you want to include a special character in a string. For example, if you want to store the price of a box of computer disks into a variable named disk_price, use the following command:


disk_price=\$5.00 (for bash and pdksh)

set disk_price = \$5.00 (for tcsh)

The backslash in this example will hide the dollar sign from the shell. If the backslash were not there, the shell would try to find a variable named 5 and perform a variable substitution on that variable. Assuming that no variable named 5 is defined, the shell assigns a value of .00 to the disk_price variable. This is because the shell substitutes a value of null for the $5 variable.


Note:  
The disk_price example could also have used single quotes to hide the dollar sign from the shell.

The back quotation marks () perform a different function. They are used when you want to use the results of a command in another command. For example, if you want to set the value of the variable contents equal to the list of files in the current directory, type the following command:


contents=‘ls‘ (for bash and pdksh)

set contents = ‘ls‘ (for tcsh)

This command executes the ls command and stores the results of the command into the contents variable. As you’ll see in the upcoming section “Iteration Statements,” this feature can be very useful when you want to write a shell program that performs some action on the results of another command.


Previous Table of Contents Next