Home > Bash > Bash: Master Command Line History

Bash: Master Command Line History

October 11th, 2009

History Expansion

Moving in the History File

emacs Mode

Command Description
CTRL-P Move to previous line
CTRL-N Move to next line
CTRL-R Search backward
ESC-< Move to first line of history file
ESC-> Move to last line of history file

vi Mode

Command Description
k or - Move backward on line
j or + Move forward one line
G Move to line given by repeat count
/string Search backward for string
?string Search forward for string
n Repeat search in same direction as previous
N Repeat search in opposite direction of previous

Event Designators

Command Description
! Start a history substitution
!! Refers to the last command
!n Refers to command line n
!-n Refers to current command line minus n
!string Refers to the most recent command starting with string
!?string? Refers to the most recent command containing string. The ending ? is optional
^string1^string2 Repeat the last command, replacing string1 with string2

Word Designators

Designator Description
0 The zeroth (first) word in a line
n The nth word in a line
^ The first argument (the second word)
$ The last argument in a line
% The word matched by the most recent ?string search
x-y A range of words from x to y. -y is synonymous with 0-y
* All words but the zeroth (first). Synonymous with 1-$. If there is only one word on the line, an empty string is returned.
x* Synonymous with x-$
x- The words form x to the second last word

The word designator follows the event designator, separated by a colon. You could repeat the previous command with different arguments by typing !!:0 followed by the new arguments.
Event designators may also be followed by modifiers.

Modifiers

Modifier Description
h Removes a trailing pathname component, leaving the head
r Removes a trailing suffix on the from.xxx
e Removes all but the trailing suffix
t removes all leading pathname components, leaving the tail
p Prints the resulting command but doesn’t execute it
q Quote the substituted words, escaping further substitutions
x Quote the substituted words, breaking them into words at blanks and newlines
s/old/new/ Substitutes new for old

More than one modifier can bey separated by a colon.

Environment Variables

Command Description
HISTCMD The history number of the current command.
HISTCONTROL Controls what is entered in the command history.
HISTFILE The name of the command history file. (~/share.webdav/.history, Same history on different machines)
HISTIGNORE A list of patterns to decide what should be retained in the history list. Using a space before a command always ignore it for the history.
HISTSIZE The number of lines kept in the command history.
HISTFILESIZE The maximum number of lines kept in the history file.
HISTTIMEFORMAT Command line history with timestamp, supports format strings of strftime. (‘%F %T)

shopt Options

The shopt options are set with shopt -sarg and unset with shopt -uarg.

Option Description
cmdhist Attempt to save all lines of multiline command in a single history entry.
histreedit If readline is being used, the opportunity is given for re-editing a failed history substitution.
histverify If readline is being used, the reults of history substituion are not immediately passed to the shell parser. Instead, the resulting line is loaded into the readline editing buffer, allowing further modification.
lithist If the cmdhist option is enabled, multiline commands are saved to the history with embedded newlines rather than using semicolon seperators where possible.

Useful Articles:
15 Examples To Master Linux Command Line History

More text is coming…

Sebastian Kusnier Bash

  1. No comments yet.
  1. No trackbacks yet.