| United States-English |
|
|
|
![]() |
Shells: User's Guide: HP 9000 Computers > Chapter 20 Command Lines and Command HistoryAccessing the History File |
|
Accessing the History File allows you to access a line typed in earlier with a few key strokes, easily enter an editing mode, change the line, and re-execute it. This is possible through several mechanisms provided by the POSIX and Korn Shell: the fc command, the vi line edit mode, and the emacs and gmacs line edit mode. In Chapter 16 “Starting and Stopping the Shell”, the two shell variables HISTFILE and HISTSIZE are discussed. The history file specified by HISTFILE contains the latest commands you executed at your terminal. Every time you type a command at the prompt and press Return it is stored in this history file. HISTSIZE specifies the maximum number of commands stored in that file. For example:
If you do not set these two variables in your .profile, the shell defaults to a file named .sh_history of 128 lines. The history mechanism keeps continuous record of the most recent commands you have executed, even if you log out and back into the system many times or execute the commands in a subshell. Any command contained in HISTFILE is accessible to you for manipulation by either the fc command or line editing modes. To list the current contents of your history file, type:
A listing, comparable to this, of the most recent commands you have executed is displayed with a number beside each command. These numbers are useful for accessing the history file commands by number. The history command is an alias for fc -l. The fc command is explained in the next section. There is a built-in command, fc (fix command), special to the POSIX and Korn Shell that allows you to list your history file or run an editor on a command line from the file. Do not confuse this command with the fc (Fortran compiler) command. The syntax of the command is: fc [-e editor] [-nlr] [first] [last] fc -e - [old=new] [command] In the first line, part of the syntax indicates listing the history file. If -l, first, and last are indicated, the commands from the first string or number to the last string or number are listed. This example prints the lines 20 thru 23.
If followed by a number, as in fc -l 23, then command lines from 23 on are displayed.
Two other options are available: -r which reverses the order of the commands and -n which suppresses listing of the command numbers. For example:
With this command line, you are placed in the vi editor with the commands 24 thru 25, without command numbers. Edit the lines. When you write and exit the file, the commands in the file are immediately executed, as shown here.
If you do not specify a -l or an editor name with -e, the value of the shell parameter FCEDIT is used, if it is set; otherwise, the shell returns an error. The -l option, used with no other arguments, displays the last 16 commands:
In this next example, the second syntax line replaces an old string with a new string in the command. Here, command can be either a command name or line number. The shell makes this substitution possible by building into fc certain simple editing capabilities that are used when the -e editor that is specified is a dash -. Using that editor, surprise is replaced by neat and echoed to the screen.
An fc -e - without any arguments displays and executes the last item in the history file which is also the most recent command executed:
If you type alias for a list of aliases, you see that r is set to fc -e - such that executing r executes the last command. Since the last command just happens to be fc -e -, this re-executes the last command, echo. There are other ESC sequences that can be executed from vi mode such as ESC k, ESC -, j, +, and ESC count G. The following sequences do the following:
So, if the set of commands looked like:
and you pressed ESC k, the shell displays:
Note that the ESC seems to have no effect, and the k does not appear on the display. For every k typed after that, the shell displays one line further back in the history file, but at the same prompt. That is, only one line at time appears at the current prompt. If you go too far backwards in the history file, move forward again using the j. For example:
Note that the ps is replaced by the pwd. If you use j or k to move from a command line you are editing to another, all the changes are lost. To specify a certain line number, use ESC count G as follows:
Once you find the command line you are searching for, you can re-execute it by pressing Return, or edit it using the vi inline edit commands, and then re-execute it. There are other CTRL sequences that can be executed from emacs mode that allow you to search the history file:
So, if you used the same set of commands:
and you executed an CTRL-P the shell displays:
For every CTRL-P typed after that, the shell displays one line further back at the same prompt. Then, if you go too far backwards in the history file, come forward using the CTRL-N. For example:
If you want to specify a line with a certain string, use CTRL-R, such as:
Once you find the command line you are searching for, you simply re-execute it by typing a Return, or edit it using the emacs inline edit commands, and then re-execute it. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||