This chapter describes how to use the GNU History Library interactively, from a user's standpoint. It should be considered a user's guide. For information on using the GNU History Library in other programs, see the GNU Readline Library Manual.
When the `-o history' option to the set
builtin
is enabled (see section The Set Builtin),
the shell provides access to the command history,
the list of commands previously typed. The text of the last
HISTSIZE
commands (default 500) is saved in a history list. The shell
stores each command in the history list prior to parameter and
variable expansion
but after history expansion is performed, subject to the
values of the shell variables
HISTIGNORE
and HISTCONTROL
.
When the shell starts up, the history is initialized from the
file named by the HISTFILE
variable (default `~/.bash_history').
HISTFILE
is truncated, if necessary, to contain no more than
the number of lines specified by the value of the HISTFILESIZE
variable. When an interactive shell exits, the last
HISTSIZE
lines are copied from the history list to HISTFILE
.
If the histappend
shell option is set (see section Bash Builtin Commands),
the lines are appended to the history file,
otherwise the history file is overwritten.
If HISTFILE
is unset, or if the history file is unwritable, the history is
not saved. After saving the history, the history file is truncated
to contain no more than $HISTFILESIZE
lines. If HISTFILESIZE
is not set, no truncation is performed.
The builtin command fc
may be used to list or edit and re-execute
a portion of the history list.
The history
builtin can be used to display or modify the history
list and manipulate the history file.
When using the command-line editing, search commands
are available in each editing mode that provide access to the
history list.
The shell allows control over which commands are saved on the history
list. The HISTCONTROL
and HISTIGNORE
variables may be set to cause the shell to save only a subset of the
commands entered.
The cmdhist
shell option, if enabled, causes the shell to attempt to save each
line of a multi-line command in the same history entry, adding
semicolons where necessary to preserve syntactic correctness.
The lithist
shell option causes the shell to save the command with embedded newlines
instead of semicolons.
See section Bash Builtin Commands, for a description of shopt
.
Bash provides two builtin commands that allow you to manipulate the history list and history file.
fc
Fix Command. In the first form, a range of commands from first to last is selected from the history list. Both first and last may be specified as a string (to locate the most recent command beginning with that string) or as a number (an index into the history list, where a negative number is used as an offset from the current command number). If last is not specified it is set to first. If first is not specified it is set to the previous command for editing and -16 for listing. If the `-l' flag is given, the commands are listed on standard output. The `-n' flag suppresses the command numbers when listing. The `-r' flag reverses the order of the listing. Otherwise, the editor given by ename is invoked on a file containing those commands. If ename is not given, the value of the following variable expansion is used:fc [-e ename] [-nlr] [first] [last]
fc -s [pat=rep] [command]
${FCEDIT:-${EDITOR:-vi}}
. This says to use the
value of the FCEDIT
variable if set, or the value of the
EDITOR
variable if that is set, or vi
if neither is set.
When editing is complete, the edited commands are echoed and executed.
In the second form, command is re-executed after each instance
of pat in the selected command is replaced by rep.
A useful alias to use with the fc
command is r='fc -s'
, so
that typing `r cc' runs the last command beginning with cc
and typing `r' re-executes the last command (see section Aliases).
history
history [-c] [n] history [-anrw] [filename] history -ps argDisplay the history list with line numbers. Lines prefixed with with a `*' have been modified. An argument of n says to list only the last n lines. Options, if supplied, have the following meanings:
-w
-r
-a
-n
-c
-s
-p
HISTFILE
variable is used.
The History library provides a history expansion feature that is similar
to the history expansion provided by csh
. This section
describes the syntax used to manipulate the history information.
History expansions introduce words from the history list into the input stream, making it easy to repeat commands, insert the arguments to a previous command into the current input line, or fix errors in previous commands quickly.
History expansion takes place in two parts. The first is to determine which line from the history list should be used during substitution. The second is to select portions of that line for inclusion into the current one. The line selected from the history is called the event, and the portions of that line that are acted upon are called words. Various modifiers are available to manipulate the selected words. The line is broken into words in the same fashion that Bash does, so that several words surrounded by quotes are considered one word. History expansions are introduced by the appearance of the history expansion character, which is `!' by default. Only `\' and `'' may be used to escape the history expansion character.
Several shell options settable with the shopt
builtin (see section Bash Builtin Commands) may be used to tailor
the behavior of history expansion. If the
histverify
shell option is enabled, and Readline
is being used, history substitutions are not immediately passed to
the shell parser.
Instead, the expanded line is reloaded into the Readline
editing buffer for further modification.
If Readline is being used, and the histreedit
shell option is enabled, a failed history expansion will be
reloaded into the Readline editing buffer for correction.
The `-p' option to the history
builtin command
may be used to see what a history expansion will do before using it.
The `-s' option to the history
builtin may be used to
add commands to the end of the history list without actually executing
them, so that they are available for subsequent recall.
This is most useful in conjunction with Readline.
The shell allows control of the various characters used by the
history expansion mechanism with the histchars
variable.
An event designator is a reference to a command line entry in the history list.
!
!n
!-n
!!
!string
!?string[?]
^string1^string2^
!!:s/string1/string2/
.
!#
Word designators are used to select desired words from the event. A `:' separates the event specification from the word designator. It may be omitted if the word designator begins with a `^', `$', `*', `-', or `%'. Words are numbered from the beginning of the line, with the first word being denoted by 0 (zero). Words are inserted into the current line separated by single spaces.
0 (zero)
0
th word. For many applications, this is the command word.
n
^
$
%
x-y
*
0
th. This is a synonym for `1-$'.
It is not an error to use `*' if there is just one word in the event;
the empty string is returned in that case.
x*
x-
If a word designator is supplied without an event specification, the previous command is used as the event.
After the optional word designator, you can add a sequence of one or more of the following modifiers, each preceded by a `:'.
h
t
r
e
p
q
x
s/old/new/
&
g
gs/old/new/
,
or with `&'.
Go to the first, previous, next, last section, table of contents.