Previous | Contents | Next


Learning the shell

Writing shell scripts

Script library

SuperMan pages

Who, What, Where, Why


We are going to take a break from our script to discuss something we have been doing but have not explained yet. In this lesson we will cover quoting. Quoting is used to acomplish two goals:

  1. To control (i.e., limit) substitutions and
  2. To perform grouping of words.

We have already used quoting. In our script, the assignment of text to our constants was performed with quoting:

TITLE="System Information for $HOSTNAME"
RIGHT_NOW=$(date +"%x %r %Z")

In this case, the text is surrounded by double quote characters. The reason we use quoting is to group the words together. If we did not use quotes, bash would think all of the words after the first one were additional commands. Try this:

[me@linuxbox me]$ TITLE=System Information for $HOSTNAME

Single and double quotes

The shell recognizes both single and double quote characters. The following are equivalent:

var="this is some text"
var='this is some text'

However, there is an important difference between single and double quotes. Single quotes limit substitution. As we saw in the previous lesson, you can place variables in double quoted text and the shell still performs substitution. We can see this with the echo command:

[me@linuxbox me]$ echo "My host name is $HOSTNAME."
My host name is linuxbox.

If we change to single quotes, the behavior changes:

[me@linuxbox me]$ echo 'My host name is $HOSTNAME.'
My host name is $HOSTNAME.

Double quotes do not suppress the substitution of words that begin with "$" but they do supress the expansion of wildcard characters. For example, try the following:

[me@linuxbox me]$ echo *
[me@linuxbox me]$ echo "*"

Quoting a single character

There is another quoting character you will encounter. It is the backslash. The backslash tells the shell to "ignore the next character." Here is an example:

[me@linuxbox me]$ echo "My host name is \$HOSTNAME."
My host name is $HOSTNAME.

By using the backslash, the shell ignored the "$" symbol. Since the shell ignored it, it did not perform the substitution on $HOSTNAME. Here is a more useful example:

[me@linuxbox me]$ echo "My host name is \"$HOSTNAME\"."
My host name is "linuxbox".

As you can see, using the \" sequence allows us to embed double quotes into our text.

Other backslash tricks

If you look at the man pages for any program written by the GNU project, you will notice that in addition to command line options consisting of a dash and a single letter, there are also long option names that begin with two dashes. For example, the following are equivalent:

ls -r
ls --reverse

Why do they support both? The short form is for lazy typists on the command line and the long form is for scripts. I sometimes use obscure options and I find the long form useful if I have to review my script again months after I wrote it. Seeing the long form helps me understand what the option does, saving me a trip to the man page. A little more typing now, a lot less work later. Laziness is maintained.

As you might suspect, using the long form options can make a single command line very long. To combat this problem, you can use a backslash to get the shell to ignore a newline character like this:

ls -l \
   --reverse \
   --human-readable \

Using the backslash in this way allows us to embed newlines in our command. Note that for this trick to work, the newline must be typed immediately after the backslash. If you put a space after the backslash, the space will be ignored, not the newline. Backslashes are also used to insert special characters into our text. These are called backslash escape characters. Here are the common ones:

Escape Character


Possible Uses



Adding blank lines to text



Inserting horizontal tabs to text



Makes your terminal beep



Inserts a backslash



Sending this to your printer ejects the page

The use of the backslash escape characters is very common. This idea first appeared in the C programming language. Today, the shell, C++, perl, python, awk, tcl, and many other programming languages use this concept. Using the echo command with the -e option will allow us to demonstrate:

[me@linuxbox me]$ echo -e "Inserting several blank lines\n\n\n"

[me@linuxbox me]$ echo -e "Words\tseparated\tby\thorizontal\ttabs."

Words separated   by  horizontal  tabs
[me@linuxbox me]$ echo -e "\aMy computer went \"beep\"."

My computer went "beep".

[me@linuxbox me]$ echo -e "DEL C:\\WIN2K\\LEGACY_OS.EXE"


Previous | Contents | Top | Next

© 2000-2002, William Shotts, Jr. Verbatim copying and distribution of this entire article is permitted in any medium, provided this copyright notice is preserved.