Main index

Introducing UNIX and Linux


Processes and devices

Overview
Processes
      Process status
      Foreground and background
      Process control
      Signals
Environment
      Environment variables
      Global and local variables
      Executable scripts
Program control
      Job control
      Command history list
      Running a job at a specific time
      Running programs periodically
      Big programs
      Timing a program
      Running programs in order
Quotes and escapes
Devices
Backquotes
Summary
Exercises

Quotes and escapes

A number of characters are understood by the shell to have a special meaning, such as $, > and <, for example, which we have already used. The purpose of quotes and backslash is to enable characters that are part of the shell's reserved characters to be used in a context where they are not recognised as such. In this section we discuss the three characters ' (single quote), " (double quote), and \ (backslash). The following other characters are reserved for the shell:

< > | & * @ # ? ! - $ ( ) [ ] { } ; = % ' `

If you wish to use any of these characters in any context other than the shell defines for them, they must be either quoted or escaped. In general, if it's a single character, preceding it with a backslash will indicate that its literal value is to be used. Alternatively, if there are several such characters, enclose the whole string in single quotes:

X='hello <$Chris>'
echo $X
hello <$Chris>

There are two important points here that you need to remember. First of all, the shell strips off pairs of quotes, and matches an opening quote with its nearest possible closing match, so:

X='abc >''>&def'
echo $X
abc >>&def

This implies that a single quote cannot occur within a quoted string that is quoted using single quotes. The second point is that quotes must come in pairs. Notice what happens if they don't:

X='abc
>

At the end of the first line the shell is looking for a single quote; not having found one, it assumes that the Newline character you entered when you typed Return is part of the string, that you intended a space instead, and that you wish to continue entering the rest of the string. The > is the continuation prompt (different to $, and which you can change the prompt by setting PS2 on the following line) indicating an unfinished command. If we then complete the dialogue:

X='abc
>
def'
echo $X
abc def

The newline is not part of the string, and is replaced by a space

Double quotes can be used in the same way as single quotes, except that not all characters enclosed between them become literal. In particular, variable names preceded by $ are replaced by their values. Double quotes may be used to include a single quote in a string:

PS1="$LOGNAME's prompt "
chris's prompt

Without quotes, the shell would assign $LOGNAME to PS1 and then try to execute prompt as the next command. Having set up variables, you may wish to protect some of them to avoid accidentally changing them. The command readonly will prohibit you changing the value of a variable:

X=42
readonly X
X=99
X: read-only variable
echo $X
42

If a read-only variable has been exported, it will not be read-only for any child processes - 'read-only-ness' is not exportable.


Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck