Featured image

One of the most powerful tool to speed up productivity while working in a terminal setting has been the history command. Some of you may have used history command to find other commands you would have executed in the past. For folks who have not used it - history command basically shows all the executed commands on that machine. My motivation to write this article comes from my discussions with Engineers who often come back telling me they could’nt find certain commands they had executed in history. This article explains a few settings one can configure to make the history command work better for you in your local & production setting.

history

> history 
history: usage: history [-c] [-d offset] [n] or history -awrn [filename] or history -ps arg [arg...]

settings

  • shows the history size setting
> echo $HISTSIZE
  • shows the history file size setting
> echo $HISTFILESIZE
  • shows the history time format setting
> echo $HISTTIMEFORMAT
  • shows the history ignore setting
> echo $HISTIGNORE
  • shows the history file location
> echo $HISTFILE
  • shows the history ingestion control settings
> echo $HISTCONTROL

show terminal history

> history
.
.
  493  history
  494  clear
  495  ls
  496  whoami
  497  ls -alrt
  498  ls
  499  which brew
  500  history

set history size

> export HISTSIZE=1000 # this will store 1000 commands
> export HISTFILESIZE=1000

note

  • HISTSIZE is the number of lines or commands that are stored in memory in a history list while your bash session is ongoing.
  • HISTFILESIZE is the number of lines or commands that (a) are allowed in the history file at startup time of a session, and (b) are stored in the history file at the end of your bash session for use in future sessions.

set history format

> export HISTTIMEFORMAT='%F %T  '
> history
.
.
  493  2018-06-12 08:57:13history
  494  2018-06-12 08:57:28clear
  495  2018-06-12 08:57:28ls
  496  2018-06-12 08:57:31whoami
  497  2018-06-12 08:57:34ls -alrt
  498  2018-06-12 08:57:35ls
  499  2018-06-12 08:57:40which brew
  500  2018-06-12 08:57:42history

note

  • %F is equivalent to %Y - %m - %d
  • %T is equivalent to ( %H : %M : %S )

set history control

> export HISTCONTROL=ignoredups # this will ignore duplicates while displaying results
> export HISTCONTROL=excludedups # this will not store duplicate commands

set history ignore

> export HISTIGNORE='ls -l:pwd:date:' # this will not store `ls -l` / `pwd` / `date` in the history file

show history file location

> echo $HISTFILE # this will show the location of the current terminal's history file

clear history

> history -c

disable storing anything in history

> export HISTSIZE=0

filter on history output

> history | grep "ls"

Some settings above can be added to your .bashrc that way you wouldn’t have to set or unset any of those variables. Also for production boxes I strongly recommend to disable history for immutable infrastructure. If you are running a mutable infrastructure - I would recommend you to have good filters on history to not store passwords / usernames. These safety settings can be achieved with a few tweaks to the example section items.