Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unlimited Bash History [closed]

Tags:

bash

unix

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?

like image 527
Francis Haart Avatar asked Feb 26 '12 21:02

Francis Haart


People also ask

How do I make bash history longer?

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.

Why does bash history disappear?

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.

How do I see all bash history?

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.

Where is bash history kept?

In Bash, your command history is stored in a file ( . bash_history ) in your home directory.


1 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.

like image 159
fotinakis Avatar answered Oct 07 '22 18:10

fotinakis