next up previous contents
Next: Command History Up: The C Shell Previous: Filename Completion

Special Characters

It might become apparent at this time that the C shell recognizes a good number of characters that have special meanings (~, >, <, & and \, just to name a few). This presents a bit of a difficulty if one is not careful. To see why, try this:

% echo ~ Hi! ~
/home/normb Hi! /home/normb

It is not what one would expect, but this is the way the shell interprets command lines. After the command echo, the shell makes any substitutions of special characters, and then proceeds to fill in the rest. In the above example, the tilde was meant as an artistic touch, not the user's home directory. Unfortunately the shell has no way of interpreting a user's intentions. There are however, ways to deal with special characters. One of these is to escape the character in question. The reader might remember that escaping a character was discussed when discussing command lines that were too long. In that case a backslash (\) was used so that the shell did not interpret the enter-key as an end of command line signal. Here escaping a character is precisely the same. To escape a character simply place a backslash immediately to the left of the character - no spaces. This will work for any special character. Here is a listing of the characters in the C shell that are considered special: ; & ( ) | * ? [ ] ~ { } ! < > ^ `` ' \ ` $, as well as, whitespace (space, tab or newline). To correct the previous example, the escaping method could be used:

% echo \~ Hi! \~
~ Hi! ~

Notice that the ! character is listed as a special character above, and yet is not escaped in the example. The reason for this will become clear in the section on command history, but a brief explanation is that, alone the ! character is not special. It becomes special when followed by almost anything else - so be careful.

The C shell, like the others, allows enclosing text in quotations to prevent interpreting special characters. This is a bit nicer when the number of characters which could be misinterpreted is more than one, and especially if white space is involved. Suppose for example that a person wanted to have a word or phrase centered on the output device. Maybe a title that acted like a heading for some output. Simply typing the title like the following would not work

% echo                                 Title
Title

since the shell treats the whitespace as a single unit regardless of the amount of spaces and tabs entered. If the text (including whitespace) was placed inside of double quotation marks however, the shell will leave the whitespace alone allowing the centering on the display.

% echo ``                              Title''
                                Title

The double quotes will allow all special characters to be taken as text rather than with special meaning with the following exceptions: '' (ending double quotations), $ (variable substitution), ` (command substitution), \ (next character escape), ! (history character), and NL (newline character). The double quote character is used to signify the ternination of the quoted text, and the $ and ! characters will be covered in the following sections.

Another option is to use the right single quotation character ('), which behaves like the double quotations with the exception that only the history character (!), the newline (NL) character, and the right single quote will be recognized as special characters by the shell. The following example shows the difference between single and double quoting of text:

% ls *.gz
lotsOdata.gz
% echo ``Current shell is $SHELL Last command was !!''
Current shell is /bin/csh Last command was ls *.gz

% echo 'Current shell is $SHELL Last command was !!'
Current shell is $SHELL Last command was ls *.gz

The difference between the two commands is that the double quoted expression allows the shell to substitute for variables while the single quoted expression will not. Both allow history substitution. The decision of which type to use is dependent on the situation.


next up previous contents
Next: Command History Up: The C Shell Previous: Filename Completion

Douglas M Gingrich
Mon Apr 27 15:25:49 MDT 1998