Jay Taylor's notes
back to listing indexunix - Unlimited Bash History - Stack Overflow
[web search]-
Home
-
- Public
-
Questions -
Tags
-
Users
-
Companies
-
Collectives
-
Explore Collectives
-
-
TeamsStack Overflow for Teams – Start collaborating and sharing organizational knowledge.
Create a free Team Why Teams?
-
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?
-
6
-
7
4 Answers
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.
-
3culprit at
.bashrc
, I had that setup after on another file loaded by .bashrc..., now working! thx! Jun 18, 2014 at 1:21 -
20Add 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 -
2To retain history history, initialize the history file with your current history.– ArthurMar 26, 2015 at 23:10
-
3Bash 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.– studogNov 17, 2017 at 15:14
-
9Many thanks - the fact that setting
HISTFILESIZE
(and possiblyHISTSIZE
) will take immediate effect cannot be overemphasized.– AttieMay 21, 2018 at 11:18
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.
-
19(export PROMPT_COMMAND='history -a') can flush commands to bash history immediately. Sep 11, 2014 at 13:49
-
4Setting
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, whileHISTSIZE= HISTFILESIZE=
did the trick. CentOS 7 here.– SeetherJul 21, 2017 at 14:09 -
3@PhilippLudwig I think rather than
HISTSIZE= HISTFILESIZE=
you shouldexport HISTSIZE=
export HISTFILESIZE=
– MatzzFeb 16, 2018 at 10:32 -
7@Seether The answer clearly states "In bash 4.3 and later...". CentOS 7 has bash 4.2.x.– villapxFeb 19, 2019 at 15:06
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.
-
21Those 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. 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. Nov 6, 2015 at 15:09
-
8Keeping 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. Jan 5, 2016 at 18:53 -
2
-
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 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 Sep 26, 2018 at 11:12
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.
-
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
-
7a 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. ;) Jul 14, 2014 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). 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.
- The Overflow Blog
-
-
- Featured on Meta
-
-
-
-
-
Linked
Related
Hot Network Questions
-
Definable set in ZF that cannot be proved to be Borel
-
In Wyndham's "Confidence Trick", a sign at an Underground station in Hell is misread as "Something Avenue". What should it be?
-
Charity say that donation is matched: how does this work?
-
Early 2010s Steampunk series aired in Sy-fy channel about a girl fighting a cult
-
I need to find the obtuse angle between the following two lines