Previous | Table of Contents | Next |
The shell performs substitution when it encounters an expression that contains one or more special characters.
In the last chapter, you learned how to access a variables value using the special $ character. The process of retrieving the value of a variable is called variable substitution. In addition to this type of substitution, the shell can perform several other types of substitutions:
In this chapter, you look at each of these types of substitution in detail.
The most common type of substitution is filename substitution. It is sometimes referred to as globbing.
Filename substitution is the process by which the shell expands a string containing wildcards into a list of filenames. Table 8.1 provides the wildcards that the shell understands.
Wildcard | Description |
---|---|
* | Matches zero or more occurrences of any character |
? | Matches one occurrence of any character |
[characters] | Matches one occurrence of any of the given characters |
Tip:
Any command or script that operates on files can take advantage of filename substitution. The examples in this section use the ls command so that the results of the filename substitution are clear. You can use any command in place of the ls command.
The simplest form of filename substitution is the * character. The * tells the shell to match zero or more occurrences of any character. If given by itself, it matches all filenames. For example, the command
$ ls *
lists every file and the contents of every directory in the current directory. If there are any invisible files or directories, they are not listed. You need to specify the -a option to ls, as described in Chapter 3, &147;Working with Files.&148;
Using the * character by itself is required in many cases, but its strength lies in the fact that you can use it to match file suffixes, prefixes, or both.
Matching a File Prefix
To match a file prefix, use the * character as follows:
command prefix*
Here, command is the name of a command, such as ls, and prefix is the filename prefix you want to match. For example, the command
$ ls ch1*
matches all the files and directories in the current directory that start with the letters ch1. The output is similar to the following:
ch10-01 ch10-02 ch10-03 ch11-01 ch11-02 ch11-03
By varying the prefix slightly, you can change lists of files that are matched. For example, the command
$ ls ch10*
generates the following list of files on your system:
ch10-01 ch10-02 ch10-03
You can vary the suffix until it matches the list of files that you want to manipulate.
Matching a File Suffix
To match a file suffix, you use the * character as follows:
command *suffix
Here, command is the name of a command, such as ls, and suffix is the filename suffix you want to match. For example, the command
$ ls *doc
matches all the files and directories in the current directory that end with the letters doc:
Backup of ch10-01.doc Backup of ch10-06.doc ch10-05.doc Backup of ch10-02.doc ch10-01.doc ch10-06.doc Backup of ch10-03.doc ch10-02.doc ch10-07.doc Backup of ch10-04.doc ch10-03.doc Backup of ch10-05.doc ch10-04.doc
The command
$ ls *.doc
matches the same set of files because all the files that were matched end with the filename suffix .doc.
By varying the suffix, you can obtain the list of files you want to manipulate.
Matching Suffixes and Prefixes
You can match both the suffix and the prefix of files using the * character as follows:
command prefix*suffix
Here, command is the name of a command, such as ls, prefix is the filename prefix, and suffix is the filename suffix you want to match. For example, the command
$ ls Backup*doc
matches all the files in the current directory that start with the letters Backup and end with the letters doc:
Backup of ch10-01.doc Backup of ch10-03.doc Backup of ch10-05.doc Backup of ch10-02.doc Backup of ch10-04.doc Backup of ch10-06.doc
You can also use more than one occurrence of the * character to narrow down the matches. For example, if the command
$ ls CGI*java
matches the following files
CGI.java CGIGet.java CGIPost.java CGITester.java
and you want to list only the files that start with the characters CGI, end with java, and contain the characters st, you can use the following command:
$ ls CGI*st*java
The output is
CGIPost.java CGITester.java
Globbing is Case Sensitive
While using the * wildcard, it is important to specify the correct case for the prefix and suffix. For example, the command $ ls ch1* produces the following list of files: ch10-01 ch10-02 ch10-03 ch11-01 ch11-02 ch11-03 whereas the command $ ls CH1* does not produce the same list of files. Later in this chapter, I show you how to generate matches that are not case sensitive. |
Previous | Table of Contents | Next |