Command Line history
SYNTAX history history [n] history -c history -d offset history [-anrw] [filename] history -ps arg KEY -c Clear the history list. This may be combined with the other options to replace the history list completely. -d offset 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 `*' have been modified. An argument of n lists only the
last n lines.
When any of the `-w', `-r', `-a', or `-n' 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 `!' by default. Only `\' and `'' 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 `-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.
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, `=' or `('. !n Refer to command line n. !-n Refer to the command n lines back. !! Refer to the previous command. This is a synonym for `!-1'. !string Refer to the most recent command starting with string. !?string[?] Refer to the most recent command containing string. The trailing `?' 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 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, !! 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. % The word matched by the most recent `?string?' search. x-y A range of words; `-y' abbreviates `0-y'. * All of the words, except the 0th. 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* Abbreviates `x-$' x- Abbreviates `x-$' like `x*', but omits the last word. If a word designator is supplied without an event specification, the previous command is used as the event.Modifiers
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 `.suffix', 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 `q', but break into words at spaces, tabs, and newlines. s/old/new/ Substitute new for the first occurrence of old in the event line. Any delimiter may be used in place of `/'. The delimiter may be quoted in old and new with a single backslash. If `&' appears in new, it is replaced by old. A single backslash will quote the `&'. 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 `s', as in gs/old/new/, or with `&'.
shopt -s histappend
# Remove duplicates from history (when a new item is added).
# Increase the history size.
# Append history to ~/.bash_history. when you exit a shell
From Allan Odgaard’s excellent Working With History in Bash
set show-all-if-ambiguous on - Alters the default behavior of the completion functions. If set to ‘on’, words which have more than one possible completion cause the matches to be listed immediately instead of ringing the bell.
set completion-ignore-case on - Case insensitive matching (Readline).
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:
% which firefox
% ./foo -f foo.conf
% vi foo.c bar.c
Getting stuff from the last command:
Full line: % !! becomes: % vi foo.c bar.c Last arg : % svn ci !$ becomes: % svn ci bar.c All args : % svn ci !* becomes: % svn ci foo.c bar.c First arg: % svn ci !!:1 becomes: % svn ci foo.c
Accessing commandlines by pattern:
Full line: % !./f becomes: % ./foo -f foo.conf Full line: % vi `!whi` becomes: % vi `which firefox` Last arg : % vi !./f:$ becomes: % vi foo.conf All args : % ./bar !./f:* becomes: % ./bar -f foo.conf First arg: % svn ci !vi:1 becomes: % svn ci foo.c
Example from Kevin Lyda (Irish Linux Users' 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.
"I never cared for fashion much, amusing little seams
and witty little pleats: it was the girls I liked" ~ David
set -o history - Enable/Disable history
set history = 8 - Set the size of the history list.
fc - Fix History Command
hash - Remember the full pathname of a name argument
time - Measure Program Resource Use
Equivalent Windows Powershell command: Get-History - Get commands entered during the current session.