11.8. Filename Substitution (Wildcards)
When evaluating the command line, the shell uses metacharacters to abbreviate filenames or pathnames that match a certain set of characters, often called wildcards. The filename substitution metacharacters listed in Table 11.7 are expanded into an alphabetically listed set of filenames. The process of expanding a metacharacter into filenames is also called filename substitution, or globbing. If a metacharacter is used and there is no filename that matches it, the Korn shell treats the metacharacter as a literal character.
Table 11.7. Shell Metacharacters and Filename SubstitutionMetacharacter | Meaning |
---|
* | Matches zero or more characters | ? | Matches exactly one character | [abc] | Matches one character in the set, a, b, or c | [!abc] | Matches one character not in the set a, b, or c | [a–z] | Matches one character in the range: any character in the set between a and z | ~ | Substitutes the user's home directory for ~ | \ | Escapes or disables the metacharacter |
11.8.1 The Asterisk
The asterisk is a wildcard that matches for zero or more of any character in a filename.
Example 11.27.
1 $ ls *
abc abc1 abc122 abc123 abc2 file1 file1.bak file2 file2.bak none
nonsense noone nothing nowhere one
2 $ ls *.bak
file1.bak file2.bak
3 $ print a*c
abc
EXPLANATION
The asterisk expands to all of the files in the present working directory. All of the files are passed to ls and displayed. All files starting with zero or more characters and ending with .bak are matched and listed. All files starting with a, followed by zero or more characters, and ending in c are matched and passed as arguments to the print command.
11.8.2 The Question Mark
The question mark represents a single character in a filename. When a filename contains one or more question marks, the shell performs filename substitution by replacing the question mark with the character it matches in the filename.
Example 11.28.
1 $ ls
abc abc1 abc122 abc123 abc2 file1 file1.bak file2 file2.bak
none nonsense noone nothing nowhere one
2 $ ls a?c?
abc1 abc2
3 $ ls ??
?? not found
4 $ print abc???
abc122 abc123
5 $ print ??
??
EXPLANATION
The files in the current directory are listed. Filenames containing four characters are matched and listed if the filename starts with an a, followed by a single character, followed by a c and a single character. Filenames containing exactly two characters are listed. There are none, so the two question marks are treated as literal characters. Because there is no file in the directory called ??, the shell sends the message ?? not found. Filenames containing six characters are matched and printed, starting with abc and followed by exactly three of any character. The ksh print function gets the two question marks as an argument. The shell tries to match for any filenames with exactly two characters. There are no files in the directory that contain exactly two characters. The shell treats the question mark as a literal question mark if it cannot find a match. The two literal question marks are passed as arguments to the print command.
11.8.3 The Square Brackets
Brackets are used to match filenames containing one character from a set or range of characters.
Example 11.29.
1 $ ls
abc abc1 abc122 abc123 abc2 file1 file1.bak file2 file2.bak
none nonsense noone nothing nowhere one
2 $ ls abc[123]
abc1 abc2
3 $ ls abc[1–3]
abc1 abc2
4 $ ls [a–z][a–z][a–z]
abc one
5 $ ls [!f–z]???
abc1 abc2
6 $ ls abc12[2-3]
abc122 abc123
EXPLANATION
All of the files in the present working directory are listed. All four-character names are matched and listed if the filename starts with abc, followed by 1, 2, or 3. Only one character from the set in the brackets is matched for a filename. All four-character filenames are matched and listed if the filename starts with abc, and is followed by a number in the range from 1 to 3. All three-character filenames are matched and listed, if the filename contains exactly three lowercase alphabetic characters. All four-character files are listed if the first character is not a letter between f and z, followed by three of any character (???). Files are listed if the filenames contain abc12, followed by 2 or 3.
11.8.4 Escaping Metacharacters
To use a metacharacter as a literal character, use the backslash to prevent the metacharacter from being interpreted.
Example 11.30.
1 $ ls
abc file1 youx
2 $ print How are you?
How are youx
3 $ print How are you\?
How are you?
4 $ print When does this line \
> ever end\?
When does this line ever end?
EXPLANATION
The files in the present working directory are listed. Note the file youx. The shell will perform filename expansion on the question mark. Any files in the current directory starting with y-o-u and followed by exactly one character are matched and substituted in the string. The filename youx will be substituted in the string to read, How are youx (probably not what you wanted to happen). By preceding the question mark (?) with a backslash, it is escaped, meaning that the shell will not try to interpret it as a wildcard. The newline is escaped by preceding it with a backslash. The secondary prompt is displayed until the string is terminated with a newline. The question mark (?) is escaped to protect it from filename expansion.
11.8.5 Tilde and Hyphen Expansion
The tilde character was adopted by the Korn shell (from the C shell) for pathname expansion. The tilde by itself evaluates to the full pathname of the user's home directory. When the tilde is appended with a username, it expands to the full pathname of that user.
The hyphen character refers to the previous working directory; OLDPWD also refers to the previous working directory.
Example 11.31.
1 $ echo ~
/home/jody/ellie
2 $ echo ~joe
/home/joe
3 $ echo ~+
/home/jody/ellie/perl
4 $ echo ~–
/home/jody/ellie/prac
5 $ echo $OLDPWD
/home/jody/ellie/prac
6 $ cd –
/home/jody/ellie/prac
EXPLANATION
The tilde evaluates to the full pathname of the user's home directory. The tilde preceding the username evaluates to the full pathname of joe's home directory. The ~+ notation evaluates to the full pathname of the working directory. The ~ – notation evaluates to the previous working directory. The OLDPWD variable contains the previous working directory. The hyphen refers to the previous working directory; cd to go to the previous working directory and display the directory.
11.8.6 New ksh Metacharacters
The new Korn shell metacharacters are used for filename expansion in a way that is similar to the regular expression metacharacters of egrep and awk. The metacharacter preceding the characters enclosed in parentheses controls what the pattern matches. See Table 11.8.
Table 11.8. Regular Expression WildcardsRegular Expression | Meaning |
---|
abc?(2|9)1 | ? matches zero or one occurrences of any pattern in the parentheses. The vertical bar represents an OR condition; for example, either 2 or 9. Matches abc21, abc91, or abc1. | abc*([0–9]) | * matches zero or more occurrences of any pattern in the parentheses. Matches abc followed by zero or more digits; for example, abc, abc1234, abc3, or abc2. | abc+([0–9]) | + matches one or more occurrences of any pattern in the parentheses. Matches abc followed by one or more digits; for example, abc3 or abc123. | no@(one|ne) | @ matches exactly one occurrence of any pattern in the parentheses. Matches noone or none. | no!(thing|where) | ! matches all strings except those matched by any of the patterns in the parentheses. Matches no, nobody , or noone, but not nothing or nowhere. |
Example 11.32.
1 $ ls
abc abc1 abc122 abc123 abc2 file1 file1.bak file2 file2.bak none
nonsense noone nothing nowhere one
2 $ ls abc?(1|2)
abc abc1 abc2
3 $ ls abc*([1–5])
abc abc1 abc122 abc123 abc2
4 $ ls abc+([1–5])
abc1 abc122 abc123 abc2
5 $ ls no@(thing|ne)
none nothing
6 $ ls no!(one|nsense)
none nothing nowhere
EXPLANATION
All the files in the present working directory are listed. Matches filenames starting with abc and followed by zero characters or one of either of the patterns in parentheses. Matches abc, abc1, or abc2. Matches filenames starting with abc and followed by zero or more numbers between 1 and 5. Matches abc, abc1, abc122, abc123, and abc2. Matches filenames starting with abc and followed by one or more numbers between 1 and 5. Matches abc1, abc122, abc123, and abc2. Matches filenames starting with no and followed by exactly thing or ne. Matches nothing or none. Matches filenames starting with no and followed by anything except one or nsense. Matches none, nothing, and nowhere.
11.8.7 The noglob Variable
If the noglob variable is set, filename substitution is turned off, meaning that all metacharacters represent themselves; they are not used as wildcards. This can be useful when searching for patterns containing metacharacters in programs like grep, sed, or awk. If noglob is not set, all metacharacters must be escaped with a backslash if they are not to be interpreted.
Example 11.33.
1 % set -o noglob # or set -f
2 % print * ?? [] ~ $LOGNAME
* ?? [] /home/jody/ellie ellie
3 % set +o noglob # or set +f
EXPLANATION
The noglob variable is set. It turns off the special meaning of the wildcards for filename expansion. You can use the –f option to set the command to achieve the same results. The filename expansion metacharacters are displayed as themselves without any interpretation. Note that the tilde and the dollar sign are still expanded. The noglob option is reset. Filename metacharacters will be expanded.
|