Command Line history
history [n] history -c
history -d offset
history [-anrw] [filename] history -ps arg
-c Clear the history list. This may be combined with
the other options to replace the history list completely.
Delete the history entry at position offset.
offset should be specified as it appears when the history is displayed.
-a Append the new history lines (history lines entered since
the beginning of the current Bash session) to the history file.
-n Append the history lines not already read from the history file
to the current history list. These are lines appended to the
history file since the beginning of the current Bash session.
-r Read the current history file and append its contents to the history list.
-w Write out the current history to the history file.
-p Perform history substitution on the args and display the result
on the standard output, without storing the results in the history list.
-s The args are added to the end of the history list as a single entry.
With no options, display the history list with line numbers. Lines prefixed with with a &qt;*&qt;&qt; have been modified. An argument of n lists only the last n lines.
When any of the &qt;-w&qt;&qt;, &qt;-r&qt;&qt;, &qt;-a&qt;&qt;, or &qt;-n&qt;&qt; options are used, if filename is given, then it is used as the history file. If not, then the value of the HISTFILE variable is used.
Recalling a previous command
Pressing the UP arrow will return to previous commands.
To return to a previously entered command, type ctrl-r and then begin typing the command. This will finish the command for you as you type. If you can remember to use ctrl-r, it will become invaluable for repeating longer commands.
To find a specific command among many previous commands; pipe history through grep:
history|grep -i first few letters of command
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 (several words surrounded by quotes are considered one word).
History expansions are introduced by the appearance of the history expansion character, which is &qt;!&qt;&qt; by default. Only &qt;&qt;&qt; and &qt;&qt; may be used to escape the history expansion character.
Several shell options settable with the shopt builtin may be used to tailor the behavior of history expansion.
The &qt;-p&qt;&qt; option to the history builtin command may be used to see what a history expansion will do before using it.
The &qt;-s&qt;&qt; 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.
An event designator is a reference to a command line entry in the history list.
! Start a history substitution, except when followed by a space,
tab, the end of the line, &qt;=&qt;&qt; or &qt;(&qt;&qt;.
!n Refer to command line n.
!-n Refer to the command n lines back.
!! Refer to the previous command. This is a synonym for &qt;!-1&qt;&qt;.
!string Refer to the most recent command starting with string.
!?string[?] Refer to the most recent command containing string.
The trailing &qt;?&qt;&qt; may be omitted if the string is followed
immediately by a newline.
^string1^string2^ Quick Substitution. Repeat the last command, replacing string1
with string2. Equivalent to !!:s/string1/string2/.
!# The entire command line typed so far. Word Designators
Word designators are used to select desired words from the event. A &qt;:&qt;&qt; separates the event specification from the word designator. It may be omitted if the word designator begins with a &qt;^&qt;&qt;, &qt;$&qt;&qt;, &qt;*&qt;&qt;, &qt;-&qt;&qt;, or &qt;&qt;&qt;&qt;. 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.
!! designates the preceding command. When you type this, the
preceding command is repeated in toto.
!!:$ designates the last argument of the preceding command.
This may be shortened to !$.
!fi:2 designates the second argument of the most recent command
starting with the letters fi.
Here are the word designators:
0 (zero) The 0th word. For many applications, this is the command word.
n The nth word.
^ The first argument; that is, word 1.
$ The last argument.
&qt; The word matched by the most recent &qt;?string?&qt;&qt; search.
x-y A range of words; &qt;-y&qt;&qt; abbreviates &qt;0-y&qt;&qt;.
* All of the words, except the 0th. This is a synonym for &qt;1-$&qt;&qt;.
It is not an error to use &qt;*&qt;&qt; if there is just one word in the event;
the empty string is returned in that case.
x* Abbreviates &qt;x-$&qt;&qt;
x- Abbreviates &qt;x-$&qt;&qt; like &qt;x*&qt;&qt;, but omits the last word.
If a word designator is supplied without an event specification,
the previous command is used as the event.Modifiers
After the optional word designator, you can add a sequence of one or more of the following modifiers, each preceded by a &qt;:&qt;&qt;.
h Remove a trailing pathname component, leaving only the head.
t Remove all leading pathname components, leaving the tail.
r Remove a trailing suffix of the form &qt;.suffix&qt;&qt;, leaving the basename.
e Remove all but the trailing suffix.
p Print the new command but do not execute it.
q Quote the substituted words, escaping further substitutions.
x Quote the substituted words as with &qt;q&qt;&qt;, but break into words at
spaces, tabs, and newlines.
Substitute new for the first occurrence of old in the event line.
Any delimiter may be used in place of &qt;/&qt;&qt;. The delimiter may be
quoted in old and new with a single backslash.
If &qt;&&qt;&qt; appears in new, it is replaced by old.
A single backslash will quote the &qt;&&qt;&qt;.
The final delimiter is optional if it is the last character on the input line.
& Repeat the previous substitution.
g Cause changes to be applied over the entire event line.
Used in conjunction with &qt;s&qt;&qt;, as in gs/old/new/, or with &qt;&&qt;&qt;.Examples (bang commands)
The following bang commands
work in not just bash but also tcsh and zsh too.
Not every bang command will work in every shell, but these are pretty universal .
assume these are the last three commands you ran:
&qt; which firefox
&qt; ./foo -f foo.conf
&qt; vi foo.c bar.c
Getting stuff from the last command:
Full line: &qt; !! becomes: &qt; vi foo.c bar.c
Last arg : &qt; svn ci !$ becomes: &qt; svn ci bar.c
All args : &qt; svn ci !* becomes: &qt; svn ci foo.c bar.c
First arg: &qt; svn ci !!:1 becomes: &qt; svn ci foo.cAccessing commandlines by pattern:
Full line: &qt; !./f becomes: &qt; ./foo -f foo.conf
Full line: &qt; vi &qt;!whi&qt; becomes: &qt; vi &qt;which firefox&qt;
Last arg : &qt; vi !./f:$ becomes: &qt; vi foo.conf
All args : &qt; ./bar !./f:* becomes: &qt; ./bar -f foo.conf
First arg: &qt; svn ci !vi:1 becomes: &qt; svn ci foo.cExample from Kevin Lyda (Irish Linux Users&qt;&qt; Group)
Various shells have options that can affect this. Be careful with shells that let you share history among instances. Some shells also
allow bang commands to be expanded with tabs or expanded and reloaded on the command line for further editing when you press return.