Bash history

Updated 29 October 2024

History commands

CommandDescription
historyDisplay history
history nDisplay only the last n commands from the history
history -cClears your current history and starts adding commands from scratch
history -d nDelete the specified nth command from history
sudo !!Run the previous command with sudo
<space> commandPrevent history from adding the command to history
shopt -s histverifyDon't execute expanded result immediately
echo $HISTSIZEDisplay maximum of commands that can be stored in history file

History modifiers

ModifiersDescription
<word>:hRemove a trailing file name component, leaving only the head from the expanded command.
<word>:rRemove a trailing suffix of the form .xxx, leaving the basename.
<word>:tRemove all leading file name components, leaving the tail.
<word>:eRemove all but the trailing suffix.
<word>:pPrint the new command but do not execute it.
<word>:qQuote the substituted words, escaping further substitutions.
<word>:xQuote the substituted words as with q, but break into words at blanks and newlines. The q and x modifiers are mutually exclusive; the last one supplied is used.
<word>:s/old/new/Substitute new for the first occurrence of old in the event line.
<word>:gs/old/new/Cause changes to be applied over the entire event line. This is used in conjunction with :s'
<word>:&Repeat the previous substitution.
<word>:GApply the following s' or &' modifier once to each word in the event line.

History slices

SlicesDescription
!!:nExpand only nth token from most recent command (command is 0; first argument is 1)
!^Expand first argument from most recent command
!$Expand last token from most recent command
!!:n-mExpand range of tokens from most recent command
!!:n-$Expand nth token to last from most recent command

History event designators

EventDescription
!!Expand the previous command
!-nExpand nth most recent command
!nExpand nth command in history
!<command>Expand most recent invocation of command <command>
!<text>Expand the last command that begins with a specific text
!n:p or !<text>:pPrint the expanded command before executing it.
^string1^string2Expand the last command, replacing string1 with string2.
!#Expand the entire command line typed so far.

History Word designators

Word designators are used to select desired words from the event. A colon “:” separates the event specification from the word designator. Here are some word designators (^, $, *, -, or %)
WordDescription
!$Expand last parameter of most recent command
!*Expand all parameters of most recent command
!^Expand the first parameter of the most recent command
!nExpand nth command in history
!%The first word matched by the most recent `?string?' search, if the search string begins with a character that is part of a word.
!x-yA range of words; -y abbreviates 0-y
Following the optional word designator, one or more of the modifiers listed on the left side may appear, each preceded by a ':'. These change or edit the word or words chosen from the history event. Remove a trailing file name component, leaving only the head from the expanded command. $ cat demo.sh $ $!:h:p #⇒ this will print demo

Spotted a mistake or want something added? Send me a note.