The C Shell permits you to do variable and file name substitutions.
The following sections offer information about creating and substituting variables in the C shell:
The C shell maintains a set of variables, each of which has as its value a list of zero or more words. Some of these variables are set by the shell or referred to by it. For instance, the argv variable is an image of the shell variable list, and words that comprise the value of this variable are referred to in special ways.
You can change and display the values of variables with the set and unset commands. Of the variables referred to by the shell, a number are toggles (variables that turn something on and off). The shell does not care what their value is, only whether they are set or unset. For instance, the verbose shell variable is a toggle that causes command input to be echoed. The setting of this variable results from issuing the -v flag on the command line.
Other operations treat variables numerically. The @ command performs numeric calculations and the result is assigned to a variable. Variable values are, however, always represented as (zero or more) strings. For numeric operations, the null string is considered to be zero, and the second and subsequent words of multiword values are ignored.
When you issue a command, the shell parses the input line and performs alias substitution. Next, before running the command, it performs variable substitution. The $ (dollar sign) character keys the substitution. It is, however, passed unchanged if followed by a blank, tab, or new-line character. Preceding the $ character with a \ (backslash) prevents this expansion, except in two cases:
The shell recognizes input and output redirection before variable expansion, and expands each separately. Otherwise, the command name and complete argument list expands together. It is therefore possible for the first (command) word to generate more than one word, the first of which becomes the command name and the rest of which become parameters.
Unless enclosed in " " (double quotation marks) or given the :q modifier, the results of variable substitution may eventually be subject to command and file name substitution. When enclosed by double quotation marks, a variable with a value that consists of multiple words expands to a single word or a portion of a single word, with the words of the variable's value separated by blanks. When you apply the :q modifier to a substitution, the variable expands to multiple words. Each word is separated by a blank and enclosed in double quotation marks to prevent later command or file name substitution.
The notations below allow you to introduce variable values into the shell input. Except as noted, it is an error to reference a variable that is not set with the set command.
You can apply the modifiers :gh, :gt, :gr, :h, :r, :q, and :x to the following substitutions. If { } (braces) appear in the command form, then the modifiers must appear within the braces. The current implementation allows only one : (colon) modifier on each variable expansion.
The following substitutions may not be changed with : modifiers:
The C shell provides several abridgment features to save time and keystrokes. If a word contains any of the characters * (asterisk), ? (question mark), [ ] (brackets), or { } (braces), or begins with a ~ (tilde), that word is a candidate for file name substitution. The C shell regards the word as a pattern and replaces the word with an alphabetically sorted list of file names matching the pattern.
The current collating sequence is used, as specified by the LC_COLLATE or LANG environment variables. In a list of words specifying file name substitution, an error results if no patterns match an existing file name. However, it is not required that every pattern match. Only the character-matching symbols * (asterisk), ? (question mark), and [ ] (brackets) indicate pattern-matching, or file name expansion. The ~ (tilde) and { } (braces) characters indicate file name abbreviation.
The * (asterisk) character matches any string of characters, including the null string. For example, in a directory containing the files:
a aa aax alice b bb c cc
the command echo a* prints all files names beginning with the character a :
a aa aax alice
Note: When file names are matched, the characters . (period) and / (slash) must be matched explicitly.
The ? (question mark) character matches any single character. The command:
ls a?x
lists every file name beginning with the letter a, followed by a single character, and ending with the letter x:
aax
To match a single character or a range of characters, enclose the character or characters inside of [ ] (brackets). The command:
ls [abc]
lists all file names exactly matching one of the enclosed characters:
a b c
Within brackets, a lexical range of characters is indicated by [a-z] . The characters matching this pattern are defined by the current collating sequence.
The ~ (tilde) and { (left brace) characters indicate file name abbreviation. A ~ at the beginning of a file name is used to represent home directories. Standing alone, the ~ character expands to your home directory as reflected in the value of the home shell variable. For example, the command:
ls ~
lists all files and directories located in your $HOME directory.
When followed by a name consisting of letters, digits, and - (dash) characters, the shell searches for a user with that name and substitutes that user's $HOME directory.
Note: If the ~ character is followed by a character other than a letter or / (slash), or appears anywhere except at the beginning of a word, it does not expand.
To match characters in file names without typing the entire file name, use { } (braces) around the file names. The pattern a{b,c,d}e is shorthand for abe ace ade . The shell preserves the left-to-right order and separately stores the results of matches at a low level to preserve this order. This construct may be nested. Thus:
~source/s1/{oldls,ls}.c
/usr/source/s1/oldls.c /usr/source/s1/ls.c
if the home directory for source is /usr/source . Similarly:
../{memo,*box}
../memo ../box ../mbox
Note: memo is not sorted with the results of matching *box . As a special case, the { (left brace), } (right brace), and { } (left and right braces) characters are passed undisturbed.
You can also use character classes to match file names within a range indication:
[:charclass:]
This format instructs the system to match any single character belonging to the specified class. The defined classes correspond to ctype subroutines.
Suppose you are in a directory containing the following files:
a aa aax Alice b bb c cc
Enter the following command at a C shell prompt:
The C shell lists all file names that begin with lowercase characters:
a aa aax b bb c cc
For more information about character class expressions, refer to the ed command.