Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
Shells: User's Guide: HP 9000 Computers > Chapter 20 Command Lines and Command History

Accessing the History File

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Index

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:

HISTFILE=/users/mary/.hist20
HISTSIZE=20

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:

$ history
...
20 ll -a
21 more file
22 ps
23 pwd
24 lsf

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.

Using the fc Command

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.

$ fc -l 20 23
20 ll -a
21 more file
22 ps
23 pwd

If followed by a number, as in fc -l 23, then command lines from 23 on are displayed.

$ fc -l 23
23 pwd
24 lsf
25 echo surprise
...

Two other options are available: -r which reverses the order of the commands and -n which suppresses listing of the command numbers. For example:

$ fc -e vi -n 24 25

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.

lsf
echo surprise
~
~
:wq!
/tmp/sh1111.12 2 lines 20 characters
ll -a
echo surprise
adv
file1
file2
surprise

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:

$ fc -l
21 more file
22 ps
23 pwd
24 lsf
...
33 lsf /users/guest
34 pwd
35 x=file1
36 echo $x moved to new directory
$

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.

$ echo surprise
surprise
$ fc -e - surprise=neat echo
echo neat
neat

An fc -e - without any arguments displays and executes the last item in the history file which is also the most recent command executed:

$ fc -e -
echo neat
neat
$ r
echo neat
neat

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.

Accessing the History File From vi Mode

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:

ESC k or ESC -

Recalls previous command and steps backward. Press ESC k or ESC -, then k or - to step through.

j and +

Moves forward to next command. Once you type ESC k or ESC -, type just j to step through. After using ESC k or ESC - and k or - to step back, use j or + to step forward.

ESC count G

Recalls earlier command number count. (Note that this is uppercase G.)

So, if the set of commands looked like:

$ fc -l
...
21 more file
22 ps
23 pwd
24 lsf

and you pressed ESC k, the shell displays:

$    ESC
$ k
$ pwd

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:

$ ps    j
$ pwd

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:

$     ESC20G
$ ll -a

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.

Accessing the History File From emacs Mode

There are other CTRL sequences that can be executed from emacs mode that allow you to search the history file:

CTRL-P

Specifies the previous command.

CTRL-N

Specifies the next command forward.

CTRL-R string

Specifies a search for the most recent command that contains string.

So, if you used the same set of commands:

$ fc -l
...
20 ll -a
21 more file
22 ps
23 pwd

and you executed an CTRL-P the shell displays:

$    CTRL-P
$ pwd

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:

$ ps    CTRL-N
$ pwd

If you want to specify a line with a certain string, use CTRL-R, such as:

$     CTRL-R
ll
$ ll - a

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.

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1983-1991 Hewlett-Packard Development Company, L.P.