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?
Increasing size of Bash History Bash by default keeps 500 commands in the history list. However, we can change this value using the HISTSIZE value. Similarly, the default size of the bash history file is 500. It is the maximum number of entries that are contained in the history file.
This happens if the computer is "slow" while reading/writing parts of the history. Basically how it works is that your currently history is read, the file is deleted/moved away, and a new file is created, where all the history lines are written to.
Run source . bashrc or create new sessions and in several terminal windows enter the comment #Tn in each. Then on one terminal, enter history | tail -N to see the last N lines. You should see all of the comments entered on the different terminals.
In Bash, your command history is stored in a file ( . bash_history ) in your home directory.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With