Bash - History (builtin command)

> Procedural Languages > Bash Shell and (Unix|Linux) Utilities (XCU)

1 - About

history is a Builtin Command that shows the history of the command executed.

Advertising

3 - Management

3.1 - Don't add a command in the history

A space at the start of a command will stop the command (and any password) from being stored in the history.

4 - Syntax

  • With no options, display the command history list with line numbers.
  • Lines listed with a * have been modified.
history [n]
history -c
history -d offset
history -anrw [filename]
history -p arg [arg ...]
history -s arg [arg ...]

where:

  • n will list only the last n lines.
  • filename is used as the name of the history file; if not, the value of HISTFILE is used.
  • -c: Clear the history list by deleting all the entries.
  • -d Offset delete the history entry at position offset.
  • -a Append the new history lines (history lines entered since the beginning of the current bash session) to the history file.
  • -n Read the history lines not already read from the history file into the current history list. These are lines appended to the history file since the beginning of the current bash session.
  • -r Read the contents of the history file and use them as the current history.
  • -w Write the current history to the history file, overwriting the history file’s contents.
  • -p Perform history substitution on the following args and display the result on the standard output. Does not store the results in the history list. Each arg must be quoted to disable normal history expansion.
  • -s Store the args in the history list as a single entry. The last command in the history list is removed before the args are added.
Advertising

5 - Configuration

5.1 - HISTTIMEFORMAT

If the shell variable HISTTIMEFORMAT is set and not null, it is used as a format string for strftime to display the time stamp associated with each displayed history entry.

The time stamp information associated with each history entry is written to the history file.

export HISTTIMEFORMAT="%d/%m/%y %T 

No intervening blank is printed between the formatted time stamp and the history line.

5.2 - HISTIGNORE

A colon-separated list of patterns used to decide which commands should be saved on the history list.

5.3 - histchars

Characters controlling history expansion and quick substitution.

  • The first character is the history substitution character, usually !.
  • The second is the quick substitution character, usually ^.
  • The third is the history comment character, usually #.

5.4 - HISTFILE

The value of the history file

6 - Shortcut

All shortcuts can be found with the bind command

bind -P | grep history
beginning-of-history can be found on "\e<", "\e[5~".
dynamic-complete-history can be found on "\e\C-i".
end-of-history can be found on "\e>", "\e[6~".
forward-search-history can be found on "\C-s".
history-and-alias-expand-line is not bound to any keys
history-expand-line can be found on "\e^".
history-search-backward is not bound to any keys
history-search-forward is not bound to any keys
next-history can be found on "\C-n", "\eOB", "\e[B".
non-incremental-forward-search-history can be found on "\en".
non-incremental-forward-search-history-again is not bound to any keys
non-incremental-reverse-search-history can be found on "\ep".
non-incremental-reverse-search-history-again is not bound to any keys
previous-history can be found on "\C-p", "\eOA", "\e[A".
reverse-search-history can be found on "\C-r".
vi-fetch-history is not bound to any keys
Advertising

6.1 - Reading the history: One line by one line

  • Up/Down Arrows
  • Ctrl+P

6.2 - Executing an command in the history with its id

The first number of the history output is the history id

!historyId
#  mutliple
!historyId1; !historyId2;

6.3.1 - Ctrl+R

Lets you search your command history for a search term.

Steps to search commands that included the search term 'youpi':

  • Hit Ctrl+R and type 'youpi'.
  • You will get the last command
  • If this is not what you want, type Ctrl + R
  • When the desired command appears, uses :
    • the left arrow to select it.
    • or Enter to execute it

6.3.2 - Grep

history | grep mySearchTerm

7 - Documentation / Reference