One last issue that causes its share of confusion is quotes. In Linux, there
are three kinds of quotes: double-quotes ("), single-quotes ('),
and back-quotes(``) (also called back-ticks). On most US keyboards, the
are on the same key, with the double-quotes accessed
by pressing Shift and the single-quote key. Usually this key is on the
right-hand side of the keyboard, next to the Enter key. On a US-American keyboard
the back-quote is usually in the upper left-hand corner of the keyboard, next to the 1.
To best understand the difference between the behavior of these quotes, I
need to talk about them in reverse order. I will first describe the back-quotes,
When enclosed inside back-ticks, the shell
interprets something to mean "the
output of the command inside the back-ticks." This is referred to as
substitution, as the output of the command inside the back-ticks is
substituted for the command itself. This is often used to assign the output of a
command to a variable.
As an example, lets say we wanted to keep track of how
many files are in a directory. From the command line,
we could say
The wc command gives me a word count, along with the number of lines and
number of characters. The | is a "pipe" symbol that is used to pass the output
of one command through another. In this example, the output of the ls command is
passed or piped through wc. Here, the command might come up as:
7 7 61
However, once the command is finished and the value has been output, we can
only get it back again by rerunning the command. Instead, If we said:
The entire line of output would be saved in the variable
count. If we then say echo $count, we get
7 7 61
showing me that count now contains the output of that line. If we wanted, we
could even assign a multi-line output to this variable.
We could use the ps command, like this
then we could type in
which gives us:
PID TTY TIME CMD 29519 pts/6 00:00:00 bash 12565 pts/6 00:00:00 ps
This is different from the output that ps would give when not assigned to
PID TTY TIME CMD
29519 pts/6 00:00:00 bash
12564 pts/6 00:00:00 ps
The next kind of quote, the single-quote ('), tells the system not to do
any expansion at all. Lets take the example above, but this time, use
If we were to now type
we would get
And what we got was exactly what we expected. The shell
did no expansion and
simply assigned the literal string "ls | wc" to the variable
count. This even
applies to the variable
operator "$." For example, if we simply say
what comes out on the screen is
No expansion is done at all and even the "$" is left unchanged.
The last set of quotes is the double-quote. This has partially the same
effect as single-quotes,
but to a limited extent. If we include something inside
everything loses its special meaning except for the variable
operator ($), the back-slash (\),
(`), and the double-quote itself.
Everything else takes on its absolute meaning. For example, we could say
which gives us
Wed Feb 01 16:39:30 PST 1995
This is a round-about way of getting the date, but it is good for
demonstration purposes. Plus, I often use this in shell
scripts when I want to log something and keep track of the date.
Remember that the back-tick
first expands the command (by running it) and then the echo echoes it to the screen.
That pretty much wraps up the quote characters. For details on other
characters that have special meaning to the
shell check out the section on
You can get more details from any number of references books on Linux or
UNIX in general (if you need it). However, the best way to see what's happening
is to try a few combinations and see if they behave as you expect.
Previously, I mentioned that some punctuation marks have special meaning,
such as *, ?, and [ ]. In fact, most of the other punctuation marks have special meaning,
as well. We'll get into more detail about them in the section on basic
It may happen that you forget to close the quotes, and you end up on a new line that starts
with (typically) a greater than symbol >. This is the secondary prompt (PS2) and is simply
telling you that your previous line continues. You can continue the line and the close the
quotes later, like this:
VAR="Now is the time for all good admins
> to come to the aid of their operating system."
It is as if you wrote the entire line at once.
Sometimes it is necessary to include the literal quotes in your output variable. This is a
your shell interprets the quotes before assinging the value to the variable.
To get around this you need to "escape" or "protect" the quotes using a backslash", like this: