|
|
The shell language has other special characters that perform a variety of useful functions. Some of these additional special characters are discussed in this section; others are described in ``Input and output redirection''.
Some shell commands take a long time to execute. The ampersand (``&'') is used to execute commands in background mode, thus freeing your terminal for other tasks. The general format for running a command in background mode is
command &
In the example below, the shell is performing a long search in background mode. Specifically, the grep(1) command is searching for the string ``delinquent'' in the file accounts. Notice the ``&'' is the last character of the command line:
$ grep delinquent accounts & 21940 $When you run a command in the background, the UNIX system outputs a process number; 21940 is the process number associated with the grep command in the example. You can use this number to terminate the execution of a background command. (Stopping the execution of processes is discussed in ``Executing, stopping and restarting processes''.) The prompt on the last line means that the terminal is free and waiting for your commands; grep has started running in background mode.
Running a command in background mode affects only the availability of your terminal; it does not affect the output of the command. Whether or not a command is run in background, it prints its output on your terminal screen, unless you redirect it to a file. (See ``Redirecting output with the > sign'' for details.)
If you want a command to continue running in background after you log out, you can execute it with the nohup(1) command. (This is discussed in ``Using the nohup command''.)
You can type two or more commands on one line as long as each is separated by a semicolon (``;'') or an ampersand (``&''), as follows:
command1; command2; command3The UNIX system executes the commands in the order that they appear in the line and prints all output on the screen. This process is called sequential execution.
Try this exercise to see how the ``;'' works. First, type:
cd; pwd; lsThe shell executes these commands sequentially:
The shell interprets the backslash (\) as an escape character that allows you to turn off any special meaning of the character immediately after it. To see how this works, try the following exercise. Create a two-line file called trial that contains the following text:
The all * game
was held in Summit.
Use the grep command to search for the asterisk in the file, as shown in the following example:
$ grep \* trial The all * game $The grep command finds the * in the text and displays the line in which it appears. Without the \ (backslash), the * would be expanded by the shell to match all filenames in the current directory.
Another way to escape the meaning of a special character is to use quotation marks. Single quotes (' . . . ') turn off the special meaning of any character except single quotes. Double quotes (``"'' . . . ``"'') turn off the special meaning of all characters except double quotes, the ``$'' and the ` (grave accent), which retain their special meanings within double quotes. An advantage of using quotes is that numerous special characters can be enclosed in the quotes; this can be more concise than using the backslash.
For example, if your filenamed trial also contained the line:
He really wondered why? Why???you could use the grep command to match the line with the three question marks as follows:
$ grep '???' trial He really wondered why? Why??? $If you had instead entered the command
grep ??? trialthe three question marks without single quotes would have been used as special characters that match all filenames in the current directory, if any, that consist of three characters. Another example of this is, if file trial contained the line
trialthen
grep ????? trialwould find the string trial in the file trial.
Quotes, like backslashes, are commonly used as escape characters for turning off the special meaning of the blank space. The shell interprets a space on a command line as a delimiter between the arguments of a command. Both single and double quotes allow you to escape that meaning.
For example, to locate two or more words that appear together in text, make the words a single argument (to the grep command) by enclosing them in quotes. To find the two words ``The all'' in your file trial, enter the following command line:
$ grep ´The all´ trial The all * game $grep finds the string The all and prints the line that contains it. What would happen if you did not put quotes around that string?
The ability to escape the special meaning of a space is especially helpful when you're using the banner(1) command. This command prints a message across a terminal screen in large, poster-size letters.
To execute banner, specify a message consisting of one or more arguments (in this case, usually words), separated on the command line by spaces. banner will use these spaces to delimit the arguments and print each argument on a separate line.
To print more than one argument on the same line, enclose the words, together, in double quotes. For example, to print a birthday greeting, type:
banner happy birthday to youThe command prints your message as a four-line banner. Now print the same message as a three-line banner. Type:
banner happy birthday "to you"Notice that the words to and you now appear on the same line. The space between them has lost its meaning as a delimiter.