[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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.
9.1 Bash History Facilities How Bash lets you manipulate your command history. 9.2 Bash History Builtins The Bash builtin commands that manipulate the command history. 9.3 History Expansion What it feels like using History as a user.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
When the `-o history' option to the set
builtin
is enabled (see section 4.3 The Set Builtin),
the shell provides access to the command history,
the list of commands previously typed.
The value of the HISTSIZE
shell variable is used as the
number of commands to save in a history list.
The text of the last $HISTSIZE
commands (default 500) is saved.
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').
The file named by the value of 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 the file
named by $HISTFILE
.
If the histappend
shell option is set (see section 4.2 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 may be used to display or modify the history
list and manipulate the history file.
When using command-line editing, search commands
are available in each editing mode that provide access to the
history list (see section 8.4.2 Commands For Manipulating The History).
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.
The shopt
builtin is used to set these options.
See section 4.2 Bash Builtin Commands, for a description of shopt
.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Bash provides two builtin commands which 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: ${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 6.6 Aliases).
history
history [n] history -c history -d offset history [-anrw] [filename] history -ps arg |
With no options, display the history list with line numbers. Lines prefixed with a `*' have been modified. An argument of n lists only the last n lines. Options, if supplied, have the following meanings:
-c
-d offset
-a
-n
-r
-w
-p
-s
When any of the `-w', `-r', `-a', or `-n' options is
used, if filename
is given, then it is used as the history file. If not, then
the value of the HISTFILE
variable is used.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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 4.2 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.
9.3.1 Event Designators How to specify which history line to use. 9.3.2 Word Designators Specifying which words are of interest. 9.3.3 Modifiers Modifying the results of substitution.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
An event designator is a reference to a command line entry in the history list.
!
!n
!-n
!!
!string
!?string[?]
^string1^string2^
!!:s/string1/string2/
.
!#
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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.
For example,
!!
!!:$
!$
.
!fi:2
fi
.
Here are the word designators:
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.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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 `&'.
[ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |