How to handle Bash history

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

History configuration

Where the 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 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 to be stored in 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 negative number to $HISTSIZE variable.

How to ignore commands

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

How to add timestamp to each history entry

Define desired time format using $HISTTIMEFORMAT variable to associate 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 command in history when 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 interactive shell.

# disable history limits
export HISTSIZE=-1
unset HISTFILESIZE

# define date format
export HISTTIMEFORMAT="%F %T "

# 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 overwritte it
shopt -s histappend

History command

Print last num of commands.

$ history num

Delete whole history.

$ history -c

Delete history entry num.

$ history -d num

Append new entries to the history file.

$ history -a

Read history file and update history list.

$ history -n

Command-line history key sequences

History navigation

CTRL-p get previous command from history
CTRL-n get next command from history
ALT-< get first command from history
ALT-> get currently entered command

History reverse search

CTRL-r start search as you type
CTRL-o execute current command
CTRL-x CTRL-e edit and execute current command
CTRL-g abort search

Command-line history reference

History reference

!n refer to the n command line
!!
!-1
refer to the previous command
!str refer to the most recent command in the history starting with str
!?str? refer to the most recent command in the history that contains with str
^str1^str2^ repeat last command, replacing str1 with str2

Examples

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
  PID TTY      STAT   TIME COMMAND
    1 ?        Ss     0:01 /sbin/init
    2 ?        S      0:00 [kthreadd]
    3 ?        S      0:00 [ksoftirqd/0]
[..]

Print command 355.

$ !355:p
mc

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
68
$ !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.

Milosz Galazka's Picture

About Milosz Galazka

Milosz is a system administrator working for a successful Polish company and a long time supporter of Free Software Foundation and Debian operating system.

Gdansk, Poland https://sleeplessbeastie.eu