I have already written a blog post about useful Bourne Again SHell key sequences, so today, I will touch on another important aspect of the daily routine – the history.

History configuration

Where history is stored

The location of the history file is stored inside the $HISTFILE variable. By default, it is ~/.bash_history file.

How to impose limits on the history file

The maximum number of lines stored in a history file is defined using $HISTFILESIZE variable. By default, the history length is limited to 500 lines.

The number of commands stored in the history file [as it is updated on exit] is defined using $HISTSIZE variable. By default, the history list is limited to 500 commands.

To disable these limits entirely unset $HISTFILESIZE and assign a negative number to $HISTSIZE variable.

How to ignore commands

Use $HISTIGNORE as a colon-separated list of patterns to be ignored.

How to add a timestamp to each history entry

Define wanted time format using $HISTTIMEFORMAT variable to associate a timestamp with each history entry.

Read strftime manual page to learn about the format specification.

How to control which commands are saved

Use $HISTCONTROL variable as a colon-separated list of additional options controlling which commands are saved.

Available options
  • ignorespace
    do not store the command in history when the line begins with space
  • ignoredups
    do not store consecutive duplicates
  • ignoreboth
    abbreviation for ignoredups and ignorespace
  • erasedups
    store only one command occurrence in the whole history file

Configuration file

Sample ~/.bashrc personal initialization file for an interactive shell.

# disable history limits
export HISTSIZE=-1
# define date format
# ignore the following commands
export HISTIGNORE="history*:pwd:ls*"
# space to ignore, do not store consecutive duplicates
export HISTCONTROL="ignoreboth"
# append to the history file instead of overwriting it
shopt -s histappend

History command

Print last num of commands.

$ history num

Delete the whole history.

$ history -c

Delete history entry num.

$ history -d num

Append new entries to the history file.

$ history -a

Read the history file and update the history list.

$ history -n

Command-line history key sequences

History navigation

  get the previous command from history
  get next command from history
  get the first command from history
  get currently entered command
  start the <em>search as you type</em>
  execute current command
  edit and execute current command
  abort search

Command-line history reference

History reference

  refer to the <em>n</em> command line
  refer to the previous command
  refer to the most recent command in the history starting with <em>str</em>
  refer to the most recent command in the history that contains with <em>str</em>
  repeat the last command, replacing <em>str1</em> with <em>str2</em>


Print six last commands.

$ history 6
  354  2014-10-20 23:18:23 ps ax
  355  2014-10-20 23:18:35 mc
  356  2014-10-20 23:18:41 top
  357  2014-10-20 23:18:48 cat /etc/passwd | wc -l
  358  2014-10-20 23:19:08 date +"%T %F"
  359  2014-10-20 23:20:38 dir /etc /bin /usr

Execute command number 354.

$ !354
ps ax
    1 ?        Ss     0:01 /sbin/init
    2 ?        S      0:00 [kthreadd]
    3 ?        S      0:00 [ksoftirqd/0]

Print command 355.

$ !355:p

Execute echo command using second and following parameters from command number 359.

$ echo !359:2*
echo /bin /usr
/bin /usr

Perform simple substitution.

$ !357:s/passwd/group/
cat /etc/group | wc -l
$ !359:1-:gs/\//\/usr\/local\//:p
/usr/local/etc /usr/local/bin

Additional notes

Please read bash and history manual pages.

Try BASH History Suggest Box utility that aims to make completion easier and more efficient.