Linux Bible. Christopher Negus
Чтение книги онлайн.
Читать онлайн книгу Linux Bible - Christopher Negus страница 41
Command-line completion
To save you a few keystrokes, the bash shell offers several different ways of completing partially typed values. To attempt to complete a value, type the first few characters and press Tab. Here are some of the values you can type partially from a bash shell:
Command, alias, or function If the text you type begins with regular characters, the shell tries to complete the text with a command, alias, or function name.
Variable If the text you type begins with a dollar sign ($), the shell completes the text with a variable from the current shell.
Username If the text you type begins with a tilde (~), the shell completes the text with a username. As a result, ~username indicates the home directory of the named user.
Hostname If the text you type begins with the at symbol (@), the shell completes the text with a hostname taken from the /etc/hosts file.
TIP
To add hostnames from an additional file, you can set the HOSTFILE
variable to the name of that file. The file must be in the same format as /etc/hosts
.
Here are a few examples of command completion. (When you see <Tab>, it means to press the Tab key on your keyboard.) Enter the following:
$ echo $OS<Tab> $ cd ~ro<Tab> $ userm<Tab>
The first example causes $OS
to expand to the $OSTYPE
variable. In the next example, ~ro
expands to the root user's home directory (~root/
). Next, userm
expands to the usermod
command.
Pressing Tab twice offers some wonderful possibilities. Sometimes, several possible completions for the string of characters you have entered are available. In those cases, you can check the possible ways that text can be expanded by pressing Tab twice at the point where you want to do completion.
The following shows the result you would get if you checked for possible completions on $P
:
$ echo $P<Tab><Tab> $PATH $PPID $PS1 $PS2 $PS4 $PWD $ echo $P
In this case, there are six possible variables that begin with $P
. After possibilities are displayed, the original command line returns, ready for you to complete it as you choose. For example, if you typed another P and hit Tab again, the command line would be completed with $PPID
(the only unique possibility).
Command-line recall
After you type a command, the entire command line is saved in your shell's history list. The list is stored in the current shell until you exit the shell. After that, it is written to a history file, from which any command can be recalled to be run again in your next session. After a command is recalled, you can modify the command line, as described earlier.
To view your history list, use the history
command. Enter the command without options or followed by a number to list that many of the most recent commands. For example:
$ history 8 382 date 383 ls /usr/bin | sort -a | more 384 man sort 385 cd /usr/local/bin 386 man more 387 useradd -m /home/chris -u 101 chris 388 passwd chris 389 history 8
A number precedes each command line in the list. You can recall one of those commands using an exclamation point (!
). Keep in mind that when an exclamation point is used, the command runs blind without presenting an opportunity to confirm the command you're referencing. There are several ways to run a command immediately from this list, including the following:
!n Run command number. Replace the n with the number of the command line and that line is run. For example, here's how to repeat the date command shown as command number 382 in the preceding history listing: $ !382 date Fri Jun 29 15:47:57 EDT 2019
!!—!! Run previous command. Runs the previous command line. Here's how you would immediately run that same date command: $ !! date Fri Jun 29 15:53:27 EDT 2019
!?string—? Run command containing string. This runs the most recent command that contains a particular string of characters. For example, you can run the date command again by just searching for part of that command line as follows: $ !?dat? date Fri Jun 29 16:04:18 EDT 2019
Instead of just running a history
command line immediately, you can recall a particular line and edit it. You can use the following keys or key combinations to do that, as shown in Table 3.4.
Another way to work with your history list is to use the fc
command. Type fc followed by a history line number, and that command line is opened in a text editor (vi
by default, type :wq to save and exit or :q! to just exit if you are stuck in vi
). Make the changes that you want. When you exit the editor, the command runs. You can also give a range of line numbers (for example, fc 100 105
). All of the commands open in your text editor and then run one after the other when you exit the editor.
TABLE 3.4 Keystrokes for Using Command History
Key(s) | Function Name | Description |
Arrow keys (↑ and ↓) | Step | Press the up and down arrow keys to step through each command line in your history list to arrive at the one you want. (Ctrl+P and Ctrl+N do the same functions, respectively.) |
Ctrl+R | Reverse incremental search | After you press these keys, you enter a search string to do a reverse search. As you type the string, a matching command line appears that you can run or edit. |
Ctrl+S | Forward incremental search | This is the same as the preceding function but for forward search. (It may not work in all instances.) |
Alt+P | Reverse search | After you press these keys, you enter a string to do a reverse search. Type a string and press Enter to see the most recent command line that includes that string. |
Alt+N | Forward search | This is the same as the preceding function but for forward search. (It may not work in all instances.) |
After