Unlimited Bash History

Set HISTSIZE and HISTFILESIZE in .bashrc to an empty string:

HISTSIZE= 
HISTFILESIZE=

In bash 4.3 and later you can also use HISTSIZE=-1 HISTFILESIZE=-1:

n.  Setting HISTSIZE to a value less than zero causes the history list to be
    unlimited (setting it 0 zero disables the history list).

o.  Setting HISTFILESIZE to a value less than zero causes the history file size
    to be unlimited (setting it to 0 causes the history file to be truncated
    to zero size).

bash --version to check your bash version.


After many large, ugly iterations and weird edge cases over the years, I now have a concise section of my .bashrc dedicated to this.

First, you must comment out or remove this section of your .bashrc (default for Ubuntu). If you don't, then certain environments (like running screen sessions) will still truncate your history:

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
# HISTSIZE=1000
# HISTFILESIZE=2000

Second, add this to the bottom of your .bashrc:

# Eternal bash history.
# ---------------------
# Undocumented feature which sets the size to "unlimited".
# http://stackoverflow.com/questions/9457233/unlimited-bash-history
export HISTFILESIZE=
export HISTSIZE=
export HISTTIMEFORMAT="[%F %T] "
# Change the file location because certain bash sessions truncate .bash_history file upon close.
# http://superuser.com/questions/575479/bash-history-truncated-to-500-lines-on-each-login
export HISTFILE=~/.bash_eternal_history
# Force prompt to write history after every command.
# http://superuser.com/questions/20900/bash-history-loss
PROMPT_COMMAND="history -a; $PROMPT_COMMAND"

Note: every command is written immediately after it's run, so if you accidentally paste a password you cannot just "kill -9 %%" to avoid the history write, you'll need to remove it manually.

Also note that each bash session will load the full history file in memory, but even if your history file grows to 10MB (which will take a long, long time) you won't notice much of an effect on your bash startup time.


As Jörg Beyer mentioned in his answer, HISTSIZE and HISTFILESIZE are key.

In addition, you should definitely check out the environmental variable HISTCONTROL, which lets you do cool things like not store duplicate history commands (HISTCONTROL=erasedups). There's no point having unlimited history if you have to browse through hundreds of lines of cd .. or similar.

Links: here, and working with bash history. The bash Variable FAQ is also worth browsing.


There are (at least) two relevant env vars here:

  • HISTSIZE: the number of entries that are stored in memory
  • HISTFILESIZE: the number of lines tat are stored in history file

**details are here

I think that we can agree that the term unlimited is often the same as very big (or do you have unlimited file storage?). So just set the values very large.

Tags:

Unix

Bash