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?
-
6superuser.com/questions/137438/… – tekknolagi Feb 26 '12 at 21:17
-
3Bash is a programming language and this is not about programming? – NeilG Sep 22 '19 at 14:28
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).
-
14(export PROMPT_COMMAND='history -a') can flush commands to bash history immediately. – cruelcage Sep 11 '14 at 13:49
-
4Setting
HISTSIZE= HISTFILESIZE=
results in completely disabling the bash history, at least for me. – Philipp Ludwig Feb 27 '17 at 7:53 -
7@PhilippLudwig setting
HISTSIZE=-1 HISTFILESIZE=-1
disabled my history, whileHISTSIZE= HISTFILESIZE=
did the trick. CentOS 7 here. – Seether Jul 21 '17 at 14:09 -
2@PhilippLudwig I think rather than
HISTSIZE= HISTFILESIZE=
you shouldexport HISTSIZE=
export HISTFILESIZE=
– Matzz Feb 16 '18 at 10:32 -
5@Seether The answer clearly states "In bash 4.3 and later...". CentOS 7 has bash 4.2.x. – villapx Feb 19 '19 at 15:06
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.
-
1@AquariusPower - Try what is exactly above, make the
HISTFILESIZE=
andHISTSIZE=
blank instead of -1, and make sure you don't have any other declarations of those params in a .bashrc or .bash_profile or .profile. – fotinakis Jun 17 '14 at 19:56 -
2culprit at
.bashrc
, I had that setup after on another file loaded by .bashrc..., now working! thx! – Aquarius Power Jun 18 '14 at 1:21 -
13Add it in my standard bash system, and adding current history is also nice, so after config, run
cat ~/.bash_history >>~/.bash_eternal_history
– Jean-Christophe Meillaud Aug 4 '14 at 15:44 -
2To retain history history, initialize the history file with your current history. – Arthur Mar 26 '15 at 23:10
-
3Many thanks - the fact that setting
HISTFILESIZE
(and possiblyHISTSIZE
) will take immediate effect cannot be overemphasized. – Attie May 21 '18 at 11:18
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.
-
16Those 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 '14 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 '15 at 15:09
-
6Keeping the lines in order is very useful if you reuse sequences of commands. E.g.
Ctrl-r
somecharsCtrl-o
Ctrl-o
Ctrl-o
Ctrl-o
. That's why I keep duplicates in history. – Stéphane Gourichon Jan 5 '16 at 18:53 -
1
-
6@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 usingCtrl-r
to find an old command (possibly using up and down arrow to move to neighboring commands in history), it is enough to keepCtrl
pressed and presso
multiple times to run again a previous series of commands, even in infinite loop if you want. See also GNU Readline - Wikipedia – Stéphane Gourichon Sep 26 '18 at 11:12
There are (at least) two relevant env vars here:
- HISTSIZE: the number of entries in the history file
- HISTFILESIZE: the number of lines in the history file
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.
-
25"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). – Francis Haart Feb 28 '12 at 3:25
-
6a 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 '14 at 14:36
-
2Older 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). – Brian Vandenberg Oct 9 '17 at 19:09