[ Previous | Next | Contents | Glossary | Home | Search ]
AIX Version 4.3 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.

The following sections offer information about creating and substituting variables in the C shell:

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.

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.

$Name 
${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
                         
${Name[number]}
                          Selects only some of the words from the value of the Name variable. The number is subjected to variable substitution and may consist of a single number, or two numbers separated by a - (dash). 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 * (asterisk) 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  
${#Name} Gives the number of words in the Name variable. This is useful for use 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 
${number} Equivalent to $argv[ number ] .
$* Equivalent to $argv[*] .

The following substitutions may not be changed with : modifiers:

$?name 
${?name} Substitutes the string 1 if the name variable is set; 0 (zero) if this variable is not set.
$?0 Substitutes 1 if the current input file name is known; 0 (zero) 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 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.

File Name Expansion

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.

File Name Abbreviation

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

expands to:

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

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

../{memo,*box}

might expand to:

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

Character Classes

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.

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, new-line, vertical tab, or form-feed character
upper Uppercase characters
xdigit Hexadecimal digits.

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.


[ Previous | Next | Contents | Glossary | Home | Search ]