How to: Unlimited Bash/shell history?

85

41

Is there a way to define an unlimited history in Bash ?

Dragos

Posted 2010-05-04T12:01:13.163

Reputation: 1 189

Answers

83

Add this to your .bashrc (Linux) or .bash_profile (MacOS):

export HISTFILESIZE=
export HISTSIZE=

There you go, unlimited history. Currently I have 27000 entries :)

From man bash:

If HISTFILESIZE is not set, no truncation is performed.

That means .bash_history is never truncated

Also the same seems to apply to HISTSIZE, although I couldn't find that documented.

Another neat feature I'm going to try is this:

If the HISTTIMEFORMAT variable is set, time stamps are written to the history file, marked with the history comment character, so they may be preserved across shell sessions, like the following:

export HISTTIMEFORMAT="%F %T "

Let me know if you have tried that already...

morgenrot

Posted 2010-05-04T12:01:13.163

Reputation: 946

2Warning: this causes headaches with gdb; if you set an HISTSIZE variable it will take it as a 0, thus disabling history size entirely. – Matteo Italia – 2014-12-05T08:37:51.060

2notes: you don't need export, HISTFILESIZE is in number of lines (not bytes), and history file truncating happens when you set variable HISTFILESIZE (and when shell exits). So don't set it twice in your config file with different values... – vaab – 2015-01-23T02:17:36.730

@vaab: if I don't export HISTFILE, I can't see what my histfile is using "echo $HISTFILE" in subshells, so while it's not necessary, it can be helpful and does no harm that I can detect. – Jeff Learman – 2016-11-29T22:03:31.380

3This does not actually work for many cases and bash history still gets truncated. See my answer below for a more complete solution. – fotinakis – 2014-05-21T18:34:10.283

41

(reposting my answer from https://stackoverflow.com/a/19533853/128597)

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".
# https://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.

fotinakis

Posted 2010-05-04T12:01:13.163

Reputation: 511

4The history file gets truncated when you set HISTFILESIZE, this is why you should remove any occurence of such event except the one you want. It'll be also truncated on shell exit (but that is expected). And you shouldn't need export. – vaab – 2015-01-23T02:22:32.560

3@vaab If you do not export, doing something like bash --norc will truncate the history again. – Yongwei Wu – 2016-11-02T02:58:58.127

1I use export HISTFILE="/home/$USER/hist/\uname -n``tty | tr '/' '-'`"` to keep shell history separate per session (based on hostname and tty name). Of course I have to create ~/hist directory first. – Jeff Learman – 2016-11-29T21:40:22.650

FYI, on my Fedora system, "~" doesn't work in HISTFILE; it doesn't get expanded. No idea why; it works in other contexts. – Jeff Learman – 2016-11-29T21:54:16.483

2Note you may want to check the value of PROMPT_COMMAND and not blindly append this repeatedly as it will do crazy things to your system. A null check or better yet a shell variable expansion search is probably safer. – dragon788 – 2017-06-20T21:44:43.250

1@fotinakis Why are you doing export HISTFILESIZE= instead of HISTFILESIZE= when it is default in .bashrc? Don't all variables get inherited by subshells if export is not mentioned ? – GypsyCosmonaut – 2017-07-06T21:47:15.350

3I use this to avoid the issue @dragon788 referred to: PROMPT_COMMAND="${PROMPT_COMMAND:+${PROMPT_COMMAND} ;}history -a"; – Brian Vandenberg – 2017-10-09T19:20:45.057

9

Include in ~/.bashrc:

# append a session's history on shell exit
shopt -s histappend
export HISTFILESIZE=
export HISTSIZE=

This answer satisfies the following criteria:

  1. a separate master history (no session can interrupt your history)

  2. automatic history writing (no hotkeys)

  3. infrequent writes (no appending after each command)

background

On interactive startup, if $HISTFILESIZE is set to a number, bash truncates $HISTFILE to that number. On interactive close, if the shell option histappend is set, bash appends $HISTSIZE lines to $HISTFILE, otherwise it overwrites $HISTFILE.

tips for OSX (Terminal)

Every time a tab is created in Terminal, ~/.bash_profile is read, which means bash doesn't go on to read your ~/.bashrc. Add the following line to your ~/etc/bash_profile:

# if bashrc has content, source it
[[ -s ~/.bashrc ]] && . ~/.bashrc

tips for screen

If you use screen, your configuration file is ~/.screenrc. If you want screen to record history, you just need to set it to use a login shell which will source your bash startup files (and record your history).

# use bash, make it a login shell
defshell -bash

cdosborn

Posted 2010-05-04T12:01:13.163

Reputation: 532

3

A different concept (may not be applicable) but you can have unlimited history when using shell-sink.

ChristopheD

Posted 2010-05-04T12:01:13.163

Reputation: 5 624