Previous Section
 < Day Day Up > 
Next Section


History

The BASH shell keeps a list, called a history list, of your previously entered commands. You can display each command, in turn, on your command line by pressing the UP ARROW key. The DOWN ARROW key moves you down the list. You can modify and execute any of these previous commands when you display them on your command line.

Tip 

The capability to redisplay a previous command is helpful when you've already executed a command you had entered incorrectly. In this case, you would be presented with an error message and a new, empty command line. By pressing the UP ARROW key, you can redisplay your previous command, make corrections to it, and then execute it again. This way, you would not have to enter the whole command again.

History Events

In the BASH shell, the history utility keeps a record of the most recent commands you have executed. The commands are numbered starting at 1, and a limit exists to the number of commands remembered—the default is 500. The history utility is a kind of short-term memory, keeping track of the most recent commands you have executed. To see the set of your most recent commands, type history on the command line and press ENTER. A list of your most recent commands is then displayed, preceded by a number.

$ history
1 cp mydata today
2 vi mydata
3 mv mydata reports
4 cd reports
5 ls

Each of these commands is technically referred to as an event. An event describes an action that has been taken—a command that has been executed. The events are numbered according to their sequence of execution. The most recent event has the highest number. Each of these events can be identified by its number or beginning characters in the command.

The history utility enables you to reference a former event, placing it on your command line and enabling you to execute it. The easiest way to do this is to use the UP ARROW and DOWN ARROW keys to place history events on your command line, one at a time. You needn't display the list first with history. Pressing the UP ARROW key once places the last history event on your command line. Pressing it again places the next history event on your command. Pressing the DOWN ARROW key places the previous event on the command line.

You can use certain control and meta keys to perform other history operations like searching the history list. A meta key is the ALT key, and the ESC key on keyboards that have no ALT key. The ALT key is used here. ALT-< will move you to the beginning of the history list; ALT-N will search it. CTRL-S and CTRL-R will perform incremental searches, display matching commands as you type in a search string. Table 8-2 lists the different commands for referencing the history list.

Tip 

If more than one history event matches what you have entered, you will hear a beep, and you can then enter more characters to help uniquely identify the event.

You can also reference and execute history events using the ! history command. The ! is followed by a reference that identifies the command. The reference can be either the number of the event or a beginning set of characters in the event. In the next example, the third command in the history list is referenced first by number and then by the beginning characters:

$ !3
mv mydata reports
$ !mv
mv mydata reports
Table 8-2: History Commands and History Event References

History Commands

Description

CTRL-N or DOWN ARROW

Moves down to the next event in the history list

CTRL-P or UP ARROW

Moves up to the previous event in the history list

ALT-<

Moves to the beginning of the history event list

ALT->

Moves to the end of the history event list

ALT-N

Forward Search, next matching item

ALT-P

Backward Search, previous matching item

CTRL-S

Forward Search History, forward incremental search

CTRL-R

Reverse Search History, reverse incremental search

fc event-reference

Edits an event with the standard editor and then executes it
Options
-l        List recent history events;
         same as history command
-e editor event-reference Invokes a specified editor  to edit a specific event

History Event References

!event num

References an event with an event number

!!

References the previous command

!characters

References an event with beginning characters

!?pattern?

References an event with a pattern in the event

!-event num

References an event with an offset from the first event

!num-num

References a range of events

You can also reference an event using an offset from the end of the list. A negative number will offset from the end of the list to that event, thereby referencing it. In the next example, the fourth command, cd mydata, is referenced using a negative offset, and then executed. Remember that you are offsetting from the end of the list—in this case, event 5— up toward the beginning of the list, event 1. An offset of 4 beginning from event 5 places you at event 2.

$ !-4
vi mydata

To reference the last event you use a following !, as in !!. In the next example, the command !! executes the last command the user executed—in this case, ls:

$ !!
ls
mydata today reports

History Event Editing

You can also edit any event in the history list before you execute it. In the BASH shell, you can do this two ways. You can use the command line editor capability to reference and edit any event in the history list. You can also use a history fc command option to reference an event and edit it with the full Vi editor. Each approach involves two different editing capabilities. The first is limited to the commands in the command line editor, which edits only a single line with a subset of Emacs commands. At the same time, however, it enables you to reference events easily in the history list. The second approach invokes the standard Vi editor with all its features, but only for a specified history event.

With the command line editor, not only can you edit the current command, you can also move to a previous event in the history list to edit and execute it. The CTRL-P command then moves you up to the prior event in the list. The CTRL-N command moves you down the list. The ALT-< command moves you to the top of the list, and the ALT-> command moves you to the bottom. You can even use a pattern to search for a given event. The slash followed by a pattern searches backward in the list, and the question mark followed by a pattern searches forward in the list. The n command repeats the search.

Once you locate the event you want to edit, you use the Emacs command line editing commands to edit the line. CTRL-D deletes a character. CTRL-F or the RIGHT ARROW moves you forward a character, and CTRL-B or the LEFT ARROW moves you back a character. To add text, you position your cursor and type in the characters you want.

If you want to edit an event using a standard editor instead, you need to reference the event using the fc command and a specific event reference, such as an event number. The editor used is the one specified by the shell in the EDITOR variable. This serves as the default editor for the fc command. You can assign to the EDITOR variable a different editor if you wish, such as Emacs instead of Vi. The next example will edit the fourth event, cd reports, with the standard editor and then execute the edited event:

$ fc 4

You can select more than one command at a time to be edited and executed by referencing a range of commands. You select a range of commands by indicating an identifier for the first command followed by an identifier for the last command in the range. An identifier can be the command number or the beginning characters in the command. In the next example, the range of commands 2–4 is edited and executed, first using event numbers and then using beginning characters in those events:

$ fc 2 4
$ fc vi c

fc uses the default editor specified in the FCEDIT special variable. Usually, this is the Vi editor. If you want to use the Emacs editor instead, you use the -e option and the term emacs when you invoke fc. The next example will edit the fourth event, cd reports, with the Emacs editor and then execute the edited event:

$ fc -e emacs 4

Configuring History: HISTFILE and HISTSAVE

The number of events saved by your system is kept in a special system variable called HISTSIZE. By default, this is usually set to 500. You can change this to another number by simply assigning a new value to HISTSIZE. In the next example, the user changes the number of history events saved to 10 by resetting the HISTSIZE variable:

$ HISTSIZE=10

The actual history events are saved in a file whose name is held in a special variable called HISTFILE. By default, this file is the .bash_history file. You can change the file in which history events are saved, however, by assigning its name to the HISTFILE variable. In the next example, the value of HISTFILE is displayed. Then a new filename is assigned to it, newhist. History events are then saved in the newhist file.

$ echo $HISTFILE
.bash_history
$ HISTFILE="newhist"
$ echo $HISTFILE
newhist


Previous Section
 < Day Day Up > 
Next Section
This HTML Help has been published using the chm2web software.