Читать книгу Linux Bible - Christopher Negus - Страница 101
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 you close your shell, the history list is stored in the .bash_history
file in your home directory. Up to 1,000 history commands are stored for you by default.