[ Bottom of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]

System User's Guide: Operating System and Devices

Variable and File-Name Substitution in the C Shell

The C Shell permits you to do variable and file-name substitutions.

This section discusses the following:

Variable Substitution 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.

To change and display the values of variables, use 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 examine toggles for a value, only for 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 $ character keys the substitution. It is, however, passed unchanged if followed by a blank, tab, or newline character. Preceding the $ character with a \ 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 " " or given the :q modifier, the results of variable substitution might 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 following notations 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 { } appear in the command form, then the modifiers must be placed within the braces. Only one : modifier is permitted on each variable expansion.

${Name} Replaced by the words assigned to the Name variable, each separated by a blank. Braces insulate the Name variable from any following characters that would otherwise be part of it. Shell variable names start with a letter and consist of up to 20 letters and digits, including the underline (_) character. If the Name variable does not specify a shell variable but is set in the environment, then its value is returned. The modifiers preceded by colons, as well as the other forms described here, are not available in this case.
${Name[number]} Selects only some of the words from the value of the Name variable. The number is subjected to variable substitution and might consist of a single number, or two numbers separated by a -. The first word of a variable's string value is numbered 1. If the first number of a range is omitted, it defaults to 1. If the last number of a range is omitted, it defaults to $#Name. The * symbol selects all words. It is not an error for a range to be empty if the second argument is omitted or is in a range.
${#Name} Gives the number of words in the Name variable. This can be used in a [number] as shown above. For example, $Name[$#Name].
$0 Substitutes the name of the file from which command input is being read. An error occurs if the name is not known.
${number} Equivalent to $argv[number].
$* Equivalent to $argv[*].

The following substitutions may not be changed with : modifiers:

${?name} Substitutes the string 1 if the name variable is set, zero (0) if this variable is not set.
$?0 Substitutes 1 if the current input file name is known, zero (0) if the file name is not known.
$$ Substitutes the (decimal) process number of the parent shell.
$< Substitutes a line from standard input, without further interpretation. Use this substitution to read from the keyboard in a shell procedure.

File-Name Substitution in the C Shell

The C shell provides several shortcuts to save time and keystrokes. If a word contains any of the characters *, ?, [ ], or { }, 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 alphabetized 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 *, ?, and [ ] indicate pattern-matching or file-name expansion. The tilde (~) and { } characters indicate file-name abbreviation.

File-Name Expansion

The * 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 dot (.) and / must be matched explicitly.

The ? character matches any single character. The following command:

ls a?x

lists every file name beginning with the letter a, followed by a single character, and ending with the letter x:


To match a single character or a range of characters, enclose the character or characters inside of [ ]. The following 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.

File-Name Abbreviation

The tilde (~) and { 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 following command:

ls ~

lists all files and directories located in your $HOME directory.

When the command is followed by a name consisting of letters, digits, and - 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 /, 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 { } around the file names. The pattern a{b,c,d}e is another way of writing 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 might be nested. Thus, the following:


expands to:

/usr/source/s1/oldls.c /usr/source/s1/ls.c

if the home directory for source is /usr/source. Similarly, the following:


might expand to:

../memo ../box ../mbox

Note: memo is not sorted with the results of matching *box. As a special case, the {, }, and { } characters are passed undisturbed.

Character Classes

You can also use character classes to match file names within a range indication. The following format instructs the system to match any single character belonging to the specified class:


The following classes correspond to ctype subroutines:

Character Class Definition
alnum Alphanumeric characters
alpha Uppercase and lowercase letters
cntrl Control characters
digit Digits
graph Graphic characters
lower Lowercase letters
print Printable characters
punct Punctuation character
space Space, horizontal tab, carriage return, newline, vertical tab, or form-feed character
upper Uppercase characters
xdigit Hexadecimal digits

Suppose that you are in a directory containing the following files:

a aa aax Alice b bb c cc

Type the following command at a C shell prompt:

     ls [:lower:]

Press Enter.

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.

[ Top of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]