Red Hat Linux 7 Unleashed

Red Hat Linux 7 Unleashed

By William Ball

Special Characters

Some characters have special meaning to Linux shells, so using them as part of variable names or strings causes your program to behave incorrectly. If a string contains such characters, you also have to use escape characters (backslashes) to indicate that the special characters should not be treated as special characters. Some of these characters are shown in Table 25.2.

Table 25.2. Special Shell Characters

Character Explanation
$ Indicates the beginning of a shell variable name
| Pipes standard output to next command
# Starts a comment
& Executes a process in the background
? Matches one character
* Matches one or more characters
> Output redirection operator
< Input redirection operator
` Command substitution (the backquote or backtick—the key above the Tab key on most keyboards)
>> Output redirection operator (to append to a file)
<< Wait until following end-of-input string (HERE operator)
[ ] Lists a range of characters
[a-z] All characters a through z
[a,z] Characters a or z
. filename Executes ("sources") the file filename
Space Delimiter between two words

A few characters deserve special note. They are the double quotes ("), the single quotes ('), the backslash (\), and the backtick (`), all discussed in the following sections. Also note that you can use input and output redirection from inside your shell scripts. Be sure to use output redirection with care when you're testing your shell programs, because you can easily overwrite files!

Double Quotes

If a string contains embedded spaces, you can enclose the string in double quotes (") so the shell interprets the whole string as one entity instead of more than one. For example, if you assigned the value of abc def (abc followed by one space followed by def) to a variable called x in a shell program as follows, you would get an error because the shell would try to execute def as a separate command.

Command Environment
x=abc def pdksh and bash
set x = abc def tcsh

What you need to do is surround the string in double quotes:

Command Environment
x="abc def" pdksh and bash
set x = "abc def" tcsh

The double quotes resolve all variables within the string. Here is an example for pdksh and bash:

var="test string"
newvar="Value of var is $var"
echo $newvar

Here is the same example for tcsh:

set var = "test string"
set newvar = "Value of var is $var"
echo $newvar

If you execute a shell program containing these three lines, you get the following result:

Value of var is test string

Single Quotes

You can surround a string with single quotes (') to stop the shell from resolving a variable. In the following examples, the double quotes in the preceding examples have been changed to single quotes.

pdksh and bash:

var='test string'
newvar='Value of var is $var'
echo $newvar

tcsh:

set var = 'test string'
set newvar = 'Value of var is $var'
echo $newvar

If you execute a shell program containing these three lines, you get the following result:

Value of var is $var

As you can see, the variable var did not get interpolated.

Backslash

You can use a backslash (\) before a character to stop the shell from interpreting the succeeding character as a special character. Say you want to assign a value of $test to a variable called var. If you use the following command, a null value is stored in var:

Command Environment
var=$test pdksh and bash
set var = $test tcsh

This happens because the shell interprets $test as the value of the variable test. No value has been assigned to test, so var contains null. You should use the following command to correctly store $test in var:

Command Environment
var=\$test pdksh and bash
set var = \$test tcsh

The backslash (\) before the dollar sign ($) signals the shell to interpret the $ as any other ordinary character and not to associate any special meaning to it.

Backtick

You can use the backtick (`) character to signal the shell to execute the string delimited by the backtick. This can be used in shell programs when you want the result of execution of a command to be stored in a variable. For example, if you want to count the number of lines in a file called test.txt in the current directory and store the result in a variable called var, you can use the following command:

Command Environment
var=`wc -l test.txt` pdksh and bash
set var = `wc -l test.txt` tcsh

Share ThisShare This

Informit Network