Jay Taylor's notes

back to listing index

unix - Unlimited Bash History - Stack Overflow

[web search]
Original source (stackoverflow.com)
Tags: bash configuration history shell-scripting stackoverflow.com
Clipped on: 2022-11-23

  1. Home
    1. Public
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Collectives
    7. Explore Collectives
    1. Teams
      Stack Overflow for Teams – Start collaborating and sharing organizational knowledge. Create a free Team Why Teams?
Asked 10 years, 9 months ago
Viewed 171k times
448
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.

Closed 9 years ago.

I want my .bash_history file to be unlimited. e.g. So I can always go back and see how I built/configured something, or what that nifty command was, or how some command broke something weeks ago. How do I change this setting?

jpaugh
6,38544 gold badges3636 silver badges9090 bronze badges
asked Feb 26, 2012 at 21:15
Francis Haart
4,80333 gold badges1515 silver badges1313 bronze badges

4 Answers

Sorted by:
551

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.

acdcjunior
129k3636 gold badges326326 silver badges300300 bronze badges
answered Oct 23, 2013 at 5:54
fotinakis
7,44222 gold badges2525 silver badges2828 bronze badges
  • 3
    culprit at .bashrc, I had that setup after on another file loaded by .bashrc..., now working! thx! Jun 18, 2014 at 1:21
  • 20
    Add it in my standard bash system, and adding current history is also nice, so after config, run cat ~/.bash_history >>~/.bash_eternal_history Aug 4, 2014 at 15:44
  • 2
    To retain history history, initialize the history file with your current history.
    – Arthur
    Mar 26, 2015 at 23:10
  • 3
    Bash truncates the history file during start up so you must comment out the default settings in .bashrc. Merely overriding the settings later in your own custom rc file will just leave you with the fruistrating situation where it looks like bash isn't respecting your changes to the history storage configuration.
    – studog
    Nov 17, 2017 at 15:14
  • 9
    Many thanks - the fact that setting HISTFILESIZE (and possibly HISTSIZE) will take immediate effect cannot be overemphasized.
    – Attie
    May 21, 2018 at 11:18
411

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.

Vipin
4,60333 gold badges3434 silver badges6060 bronze badges
answered Sep 2, 2012 at 9:56
Lri
26k88 gold badges8282 silver badges8080 bronze badges
  • 19
    (export PROMPT_COMMAND='history -a') can flush commands to bash history immediately.
    – cruelcage
    Sep 11, 2014 at 13:49
  • 4
    Setting HISTSIZE= HISTFILESIZE= results in completely disabling the bash history, at least for me. Feb 27, 2017 at 7:53
  • 7
    @PhilippLudwig setting HISTSIZE=-1 HISTFILESIZE=-1 disabled my history, while HISTSIZE= HISTFILESIZE= did the trick. CentOS 7 here.
    – Seether
    Jul 21, 2017 at 14:09
  • 3
    @PhilippLudwig I think rather than HISTSIZE= HISTFILESIZE= you should export HISTSIZE= export HISTFILESIZE=
    – Matzz
    Feb 16, 2018 at 10:32
  • 7
    @Seether The answer clearly states "In bash 4.3 and later...". CentOS 7 has bash 4.2.x.
    – villapx
    Feb 19, 2019 at 15:06
52

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.

Rob Bednark
24.1k2121 gold badges7878 silver badges119119 bronze badges
answered Feb 26, 2012 at 21:34
simont
65.2k1717 gold badges114114 silver badges133133 bronze badges
  • 21
    Those lines of cd give you context for the commands issued. They're actually incredibly useful. I can't understand why anyone would ever want to delete them, unless the only way you ever use history is by paging through it one command at a time.
    – iconoclast
    Jul 14, 2014 at 14:38
  • @simont I also use the ignorespace option with HISTCONTROL then use this for anything I don't want written to history, such as passwords.
    – bigtunacan
    Nov 6, 2015 at 15:09
  • 8
    Keeping the lines in order is very useful if you reuse sequences of commands. E.g. Ctrl-r somechars Ctrl-o Ctrl-o Ctrl-o Ctrl-o. That's why I keep duplicates in history. Jan 5, 2016 at 18:53
  • 2
    @StéphaneGourichon what's Ctrl-o for ? Sep 26, 2018 at 8:22
  • 10
    @CiprianTomoiagă Ctrl-o runs the command at prompt and shows right away the command right after the previous one in history. One benefit is that after using Ctrl-r to find an old command (possibly using up and down arrow to move to neighboring commands in history), it is enough to keep Ctrl pressed and press o multiple times to run again a previous series of commands, even in infinite loop if you want. See also GNU Readline - Wikipedia Sep 26, 2018 at 11:12
31

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.

Eugen Konkov
20.2k1212 gold badges9696 silver badges140140 bronze badges
answered Feb 26, 2012 at 21:22
Jörg Beyer
3,6032020 silver badges3535 bronze badges
  • 31
    "It should be noted that 1000000000 is only equal to infinity for very small values of infinity. – sepp2k" lol. yes, i am content with small values of infinity (say, a month of frequent terminal use). Feb 28, 2012 at 3:25
  • 7
    a month of frequent terminal use is an infititesimally small fraction of infinity. I would say a lifetime of frequent terminal use equates to a very very very small value of infinity. But a lifetime of frequent terminal use is good enough for my shell history. ;)
    – iconoclast
    Jul 14, 2014 at 14:36
  • 2
    Older versions of zsh at least will crash if you set HISTSIZE and/or HISTFILESIZE too large because it tries to pre-allocate enough memory. This became relevant when running tools that use zsh as their shell (dbx for example). Oct 9, 2017 at 19:09
  • There are different types of infinity: see countably infinite and uncountably infinite. Inquiring minds might be interested.. and I'm fairly sure there are a lot of those hanging around stackoverflow. Dec 14, 2021 at 22:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.