Online Book Reader

Home Category

Classic Shell Scripting - Arnold Robbins [92]

By Root 978 0
that the shell would otherwise treat as separate arguments, you have to quote the arguments. There are three ways to quote things:

Backslash escaping

Preceding a character with a backslash (\) tells the shell to treat that character literally. This is the easiest way to quote a single character:

$ echo here is a real star: \* and a real question mark: \?

here is a real star: * and a real question mark: ?

Single quotes

Single quotes ('...') force the shell to treat everything between the pair of quotes literally. The shell strips the two quotes, and otherwise leaves the enclosed text completely alone:

$ echo 'here are some metacharacters: * ? [abc] ` $ \'

here are some metacharacters: * ? [abc] ` $ \

There is no way to embed a single quote within a single-quoted string. Even backslash is not special within single quotes. (On some systems, a command like echo 'A\tB' makes it look like the shell treats backslash specially. However, it is the echo command doing the special treatment: see Table 2-2 for more information.)

If you need to mix single and double quotes, you can do so by careful use of backslash escaping and concatenation of differently quoted strings:

$ echo 'He said, "How'\''s tricks?"'

He said, "How's tricks?"

$ echo "She replied, \"Movin' along\""

She replied, "Movin' along"

Note that no matter how you do it, though, such combinations are almost always hard to read.

Double quotes

Like single quotes, double quotes ("...") group the enclosed text as a single string. However, the shell does process the enclosed text for escaped characters and for variable, arithmetic, and command substitutions:

$ x="I am x"

$ echo "\$x is \"$x\". Here is some output: '$(echo Hello World)'"

$x is "I am x". Here is some output: 'Hello World'

Within double quotes, the characters $, ", `, and \ must be preceded by a \ if they are to be included literally. A backslash in front of any other character is not special. The sequence \-newline is removed completely, just as when used in the body of a script.

Note that, as shown in the example, single quotes are not special inside double quotes. They don't have to be in matching pairs, nor do they have to be escaped.

In general, use single quotes when you want no processing done at all. Otherwise, use double quotes when you want multiple words to be treated as a single string, but you need the shell to do some work for you. For example, to concatenate the value of one variable onto another, you would use something like this:

oldvar="$oldvar $newvar" Append newvar's value to oldvar

Evaluation Order and eval

The various expansions and substitutions that we've covered are done in a defined order. The POSIX standard provides the picayune details. Here, we describe things at the level a shell programmer needs to understand things. This explanation is simplified to elide the most petty details: e.g., middles and ends of compound commands, special characters, etc.

Each line that the shell reads from the standard input or a script is called a pipeline; it contains one or more commands separated by zero or more pipe characters (|). (Actually, several special symbols separate individual commands: semicolon, ;, pipe, |, ampersand, &, logical AND, &&, and logical OR, ||.) For each pipeline it reads, the shell breaks it up into commands, sets up the I/O for the pipeline, and then does the following for each command, in the order shown:

Splits the command into tokens that are separated by the fixed set of metacharacters: space, tab, newline, ;, (, ), <, >, |, and &. Types of tokens include words, keywords, I/O redirectors, and semicolons.

It's a subtle point, but variable, command, and arithmetic substitution can be performed while the shell is doing token recognition. This is why the vi ~$user/.profile example presented earlier in Section 7.5.1, actually works as expected.

Checks the first token of each command to see if it is a keyword with no quotes or backslashes. If it's an opening keyword (if and other control-structure openers, {, or (), then

Return Main Page Previous Page Next Page

®Online Book Reader