[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]
System User's Guide: Operating System and Devices

File Name Substitution in the Korn Shell or POSIX Shell

The Korn shell, or POSIX shell, performs file name substitution by scanning each command word specified by the Word variable for certain characters. If a command word includes the * (asterisk), ? (question mark) or [ (left bracket) characters, and the -f flag has not been set, the shell regards the word as a pattern. The shell replaces the word with file names, sorted according to the collating sequence in effect in the current locale, that match that pattern. If the shell does not find a file name to match the pattern, it does not change the word.

When the shell uses a pattern for file name substitution, the . (dot) and / (slash) characters must be matched explicitly.

Note: The Korn shell does not treat these characters specially in other instances of pattern matching.

These pattern-matching characters indicate the following substitutions:

* Matches any string, including the null string.
? Matches any single character.
[...] Matches any one of the enclosed characters. A pair of characters separated by a - (hyphen) matches any character lexically within the inclusive range of that pair, according to the collating sequence in effect in the current locale. If the first character following the opening [ (left bracket) is an ! (exclamation point), then any character not enclosed is matched. A - (hyphen) can be included in the character set by putting it as the first or last character.

You can also use the [:charclass:] notation to match file names within a range indication. This format instructs the system to match any single character belonging to class. The definition of which characters constitute a specific character class is present through the LC_CTYPE category of the setlocale subroutine. All character classes specified in the current locale are recognized.

The names of some of the character classes are:

For example, [[:upper:]] matches any uppercase letter.

The Korn shell supports file name expansion based on collating elements or symbols, or equivalence classes.

A PatternList is a list of one or more patterns separated from each other with a vertical bar ( | ). Composite patterns are formed with one or more of the following:

?(PatternList) Optionally matches any one of the given patterns.
*(PatternList) Matches zero or more occurrences of the given patterns.
+(PatternList) Matches one or more occurrences of the given patterns.
@(PatternList) Matches exactly one of the given patterns.
!(PatternList) Matches anything, except one of the given patterns.

Pattern matching has some restrictions. If the first character of a file name is a . (dot), it can be matched only by a pattern that also begins with a dot. For example, * (asterisk) matches the file names myfile and yourfile but not the file names .myfile and .yourfile. To match these files, use a pattern such as the following:

.*file

If a pattern does not match any file names, then the pattern itself is returned as the result of the attempted match.

File and directory names should not contain the characters * (asterisk), ? (question mark), [ (left bracket), or ] (right bracket) because they can cause infinite recursion (that is, infinite loops) during pattern-matching attempts.

Quote Removal

The quote characters, backslash (\), single quote ('), and double quote (") that were present in the original word will be removed unless they have themselves been quoted.


[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]