Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here's my shell:

$ $SHELL --version | head -1
GNU bash, version 4.1.7(2)-release (i386-apple-darwin10.3.0)

Here are my history related settings:

export HISTCONTROL=ignoredups:erasedups
export HISTSIZE=100000
export HISTFILESIZE=100000
shopt -s histappend
shopt -s histverify ## edit a recalled history line before executing
shopt -s histreedit ## reedit a history substitution line if it failed

## Save the history after each command finishes
## (and keep any existing PROMPT_COMMAND settings)
export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"


I was able to cause the truncation by using sudo -s. On entering the sudo shell, the history is truncated to 500 entries. What rc files does sudo -s look at so I can make sure it loads my history settings?

share|improve this question
This is really cool, and related: – kch Aug 23 '10 at 19:28
up vote 3 down vote accepted

There's another variable you need to check: HISTFILESIZE

From man bash:

The maximum number of lines contained in the history file. When this variable is assigned a value, the history file is truncated, if necessary, by removing the oldest entries, to contain no more than that number of lines. The default value is 500. The history file is also truncated to this size after writing it when an interactive shell exits.


Check your startup files to make sure that they are being processed correctly and in the correct order and that the variables are being set in the correct one and not being overwritten.

My preference is not to source one "official" startup file from another one, but to have common code in a third "local" file which is sourced by each of the "official" ones and let the shell determine when they are processed (interactive or not, login or not). Beware that some distributions do have these files set up so that one sources the other so you want to make sure that you don't set up an infinite loop.

If you're using these history files more for logging than reusing previous commands take a look at my logging functions and see if they meet your needs. You can still use history - you just won't need to keep so much of it.

See the comments below for more detail.

share|improve this answer
Hum, will try that. But this default value of 500 gave me a hint (it's the same for HISTSIZE): maybe when I start a shell that doesn't set these variables, the default of 500 kicks in and truncates my history. Makes sense? – kch Aug 3 '10 at 14:31
@kch: Makes sense to me. – Dennis Williamson Aug 3 '10 at 18:18
So I changed my prompt to show the length of history and I'm keeping an eye on it to better understand what would be triggering the truncation. (However I'm now running with HISTFILESIZE set too.) Any sure-fire way to enter a shell that would not have my history settings? (so I can try to test my theory and force a truncation) – kch Aug 6 '10 at 15:00
@kch: Try bash --norc --noprofile – Dennis Williamson Aug 6 '10 at 15:03
So bash --norc --noprofile didn't trigger the issue, but sudo -s did. I updated the question accordingly. – kch Aug 23 '10 at 6:27

And you seem to be rather sure, that it wasn't you yourself who (accidentally) deleted it... Are you the only user on this computer?

So this is highly unusual. Make sure nobody is tampering with your bash_history. Maybe you can get some inspiration here:

share|improve this answer
Yeah, just me. No way I deleted the file. This happens quite regularly. I don't even think the file is ever deleted, just truncated. – kch Aug 3 '10 at 11:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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