-->
Previous | Table of Contents | Next |
Another way that bash makes typing commands easier is by enabling users to use wildcards in their commands. The bash shell supports three kinds of wildcards:
The * wildcard can be used in a manner similar to command-line completion. For example, assume the current directory contains the following files:
News/ bin/ games/ mail/ samplefile test/
If you want to cd into the test directory, you can type cd test or use command-line completion:
cd t<tab>
This causes bash to complete the command for you. Now, theres a third way to do the same thing. Because only one file begins with the letter t, you can also change to the directory by using the * wildcard. To do that enter the following command:
cd t*
The * matches any character and any number of characters, so the shell replaces the t* with test (the only file in the directory that matches the wildcard pattern).
This only works reliably if there is one file in the directory that begins with the letter t. If more than one file in the directory begins with the letter t, the shell tries to replace t* with the list of filenames in the directory that match the wildcard pattern. The cd command cds into the first directory in this list, which is listed alphabetically, and may or may not be the intended file.
A more practical situation in which to use the * wildcard is when you want to execute the same command on multiple files that have similar filenames. For example, assume the current directory contains the following files:
ch1.doc ch2.doc ch3.doc chimp config mail/ test/ tools/
If you want to print all of the files that have a .doc extension, you can easily do so by entering the following command:
lpr *.doc
In this case, bash replaces *.doc with the names of all of the files in the directory that match that wildcard pattern. After bash performs this substitution, the command that is processed is:
lpr ch1.doc ch2.doc ch3.doc
The lpr command can be invoked with the arguments of ch1.doc, ch2.doc, and ch3.doc.
Note:
Given the directory contents used in the previous example, there are several ways to print all of the files that have a .doc extension. Any of the following commands also work:lpr *doc lpr *oc lpr *c
The ? wildcard functions in an identical way to the * wildcard except that the ? wildcard only matches a single character. Using the same directory contents shown in the previous example, the ? wildcard can be used to print all of the files with the .doc extension by entering the following command:
lpr ch?.doc
The [ ] wildcard enables you to specify certain characters or ranges of characters to match. To print all of the files in the example with the .doc extension using the [ ] wildcard, enter one of the following two commands:
lpr ch[123].doc
Using a command to specify a range of characters, enter:
lpr ch[1-3].doc
bash also supports command history. This simply means that bash keeps track of a certain number of previous commands that have been entered into the shell. The number of commands is given by a shell variable called HISTSIZE. For more information on HISTSIZE, see the bash Variables section later in this chapter.
bash stores the text of the previous commands in a history list. When you log in to your account, the history list is initialized from a history file. The filename of the history file can be set using the HISTFILE bash variable. The default filename for the history file is .bash_history. This file is usually located in your home directory. (Notice that the file begins with a period. This means that the file is hidden and only appears in a directory listing if you use the -a or -A option of the ls command.)
Just storing previous commands into a history file is not all that useful, so bash provides several ways of recalling them. The simplest way of using the history list is with the up- and down-arrow keys, which scroll through the commands that have been previously entered.
Pressing the up-arrow key causes the last command that was entered to appear on the command line. Pressing the up-arrow again puts the command previous to that one on the command line, and so on. If you move up in the command buffer past the command that you want, you can also move down the history list a command at a time by pressing the down-arrow. (This is the same process used by the DOS doskey utility.)
The command displayed on the command line through the history list can be edited, if needed. bash supports a complex set of editing capabilities that are beyond the scope of this book, but there are simple ways of editing the command line for small and easy changes. You can use the left- and right-arrow keys to move along the command line. You can insert text at any point in the command line and delete text by using the Backspace or Delete key. Most users will find these simple editing commands sufficient.
Note:
The complex set of editing commands that bash offers is similar to the commands used in the emacs and vi text editors.
Another method of using the history file is to display and edit the list using the history and fc (fix command) commands built in to bash. The history command can be invoked using two different methods. The first method uses the command:
history [n]
Previous | Table of Contents | Next |