Выбрать главу

► $0 — The name of the shell program

► $* — A single string of all arguments passed at the time of invocation of the shell program

To show these built-in variables in use, here is a sample program called mypgm2:

#!/bin/sh

#my test program

echo "Number of parameters is $#"

echo "Program name is $0"

echo "Parameters as a single string is $*"

If you execute mypgm2 from the command line in bash as follows

$ bash mypgm2 Andrew Hudson

you get the following result:

Number of parameters is 2

Program name is mypgm2

Parameters as a single string is Sanjiv Guha

Special Characters

Some characters have special meaning to Linux shells; these characters represent commands, denote specific use for surrounding text, or provide search parameters. Special characters provide a sort of shorthand by incorporating these rather complex meanings into a simple character. Table 33.1 shows some special characters.

TABLE 33.1 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)
[ ] Range of characters
[a-z] All characters a through z
[a,z] or [az] Characters a or z
Space Delimiter between two words

Special characters are very useful to you when you are creating shell scripts, but if you inadvertently use a special character as part of variable names or strings, your program behaves incorrectly. As you learn in later parts of this section, you can use one of the special characters in a string if you precede it with an escape character (/, or backslash) to indicate that it isn't being used as a special character and shouldn't be treated as such by the program.

A few special characters deserve special note. They are the double quotes ("), the single quotes ('), the backslash (\), and the backtick (`) — all discussed in the following sections.

Using Double Quotes to Resolve Variables in Strings with Embedded Spaces

If a string contains embedded spaces, you can enclose the string in double quotes (") so that 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 bash
set x=abc def tcsh

The shell executes the string as a single command if you surround the string in double quotes, as follows:

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

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

var="test string"

newvar="Value of var is $var"

echo $newvar

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

Value of var is test string

Using Single Quotes to Maintain Unexpanded Variables

You can surround a string with single quotes (') to stop the shell from expanding variables and interpreting special characters. When used for the latter purpose, the single quote is an escape character, similar to the backslash, which you learn about in the next section. Here, you learn how to use the single quote to avoid expanding a variable in a shell script. An unexpanded variable maintains its original form in the output.

In the following examples, the double quotes in the preceding examples have been changed to single quotes:

var='test string'

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