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 can 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 command history list with line numbers. Lines listed with a * have been modified.
An argument of n lists only the last n lines.
If filename is supplied, it is used as the name of the history file; if not, the value of HISTFILE is used (-anrw options).
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 ''' can be used to escape the history expansion character.
Several shell options settable with the shopt builtin can be used to tailor the behavior of history expansion.
The '-p' option to the history builtin command can be used to see what a history expansion will do before using it.
The '-s' option to the history builtin can 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 '?' can 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 can 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 can 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.
After the optional word designator, you can add a sequence of one or more of the following modifiers, each preceded by a ':'.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 can be used in place of '/'. The delimiter can 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 (made with s/old/new/ ). If "&" is used before a transformation has been stored you will get the error 'no 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).
HISTCONTROL controls how bash stores command history. The ignorespace flag will ignore commands that start with spaces (prefix with a space any commands that you don't added to history), ignoredups, will ignore duplicates and ignoreboth will ignore both spaces and duplicates. equivalent to: ignorespace:ignoredups.
HISTFILE defines the history file, by default ~/.bash_history
HISTSIZE defines the number of lines of history to save in memory (by default 500). The history file is truncated, if necessary, by removing the oldest entries, to contain no more than that number of lines
HISTFILESIZE defines the number of lines of history to save in the history file (~/.bash_history) when the shell exits (by default 500).
If the shell variable HISTTIMEFORMAT is set and not null, it is used as a strftime format string to display the time stamp associated with each displayed history entry. No intervening blank is printed between the formatted time stamp and the history line. In addition, the time stamp information associated with each history entry will be written to the history file.
The return value is 0 unless an invalid option is encountered, an error occurs while reading or writing the history file, an invalid offset is supplied as an argument to -d, or the history expansion supplied as an argument to -p fails.
Examples (bang commands)
Substitutions and using & to repeat previous substitutions:
$ echo demo64 demo64 $ !:s/demo/bin echo bin64 bin64 $ echo demo100 demo100 $ !:& echo bin100 bin100
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, example from Kevin Lyda (Irish Linux Users' Group):
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
Save History, after executing a series
of commands, save them all:
$ history > cmds.txt
This is a BASH shell builtin, to display your local syntax from the bash prompt type: help history
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.
"The past is a foreign country; they do things differently there" ~ L.P. Hartley (The Go-Beteween)
Related linux commands:
set -o history - Enable/Disable history.
set history = 8 - Set the size of the history list.
fc - Fix History Command.
bashrc - Set history commands at every login.
bind - Set or display readline key bindings to recall history.
hash - Remember the full pathname of a name argument.
time - Measure Program Resource Use.
Equivalent Windows PowerShell cmdlet: Get-History - Get commands entered during the current session.