Programming with the UNIX system shell

Special characters

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''.

Running a command in background with the ampersand

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 &

NOTE: You should not run interactive shell commands, such as read, in the background.

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 &
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''.)

Executing commands sequentially with the semicolon

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; command3
The 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; ls
The shell executes these commands sequentially:

  1. cd changes your location to your login directory

  2. pwd prints the full pathname of your current directory

  3. ls lists the files in your current directory
If you want to save the system responses to these commands, (or prevent them from appearing on your screen), you can redirect them to a file. See ``Input and output redirection'' for instructions.

Turning off special meanings with the backslash

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.

Turning off special meanings with quotation marks

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 ??? trial
the 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
   grep ????? trial
would find the string trial in the file trial.

Turning off the meaning of a space with quotes

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 you
The 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.

Next topic: Input and output redirection
Previous topic: Summary of filename generation characters

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004