8

My .bash_history keeps truncating even though I set:

export HISTSIZE=
export HISTFILESIZE=

On my .bash_profile. Im not sure when it happens but it might happen when I logout or shut down my computer. Since I have had this problem for a while, I tried setting the same variables on my /etc/profile, which doesn't fix the problem.

It does go up to 2000 lines (which is the max I saw before it truncated) but sometimes it just truncates and I notice that later. What can I do to fix this?

DisplayName
  • 11,688

2 Answers2

9

.bash_profile is only loaded if bash is started as a login shell. On OSX, the default is to start a login shell in every terminal, so that's the common case for you. But if you ever type bash to run a nested shell, or run screen or tmux, or anything else that starts an interactive shell, you'll end up with an interactive, non-login shell. This shell only reads ~/.bashrc, it doesn't read ~/.bash_profile. Since you exported HISTFILESIZE to the environment, a shell that's started from a program started from a terminal that ran a login shell will inherit that setting. But a shell started independently will not (which is a fundamental flaw in OSX's approach of starting login shells in terminals.) So a shell started inside a terminal emulator that doesn't start a login shell, or in a screen session started by a cron job, or in Aquamacs, etc. won't have any HISTSIZE or HISTFILESIZE setting and thus will use the default values.

The fix is to set HISTSIZE and HISTFILESIZE at the proper place: in ~/.bashrc.

Bash bizarrely reads .bashrc only from non-login interactive shells. To get it read in all interactive shells, put the following line in your .bash_profile:

case $- in *i*) . ~/.bashrc;; esac
  • I will try setting it in bashrc and see how it goes. And what do you mean with the first part of your answer? I never said anything about HISTFILE or misreported anything. – DisplayName Nov 28 '14 at 00:22
  • unfortunately this did not work, it was trimmed when I shut down my computer today. – DisplayName Nov 29 '14 at 21:59
  • Since you exported HISTFILESIZE to the environment, a shell that's started from a program started from a terminal that ran a login shell will inherit that setting. I think I got a little dizzy reading that. I've been having the same problem since forever. I ended up dealing with via an autobackup, but I'll give your suggestion a try. – tel Jul 24 '17 at 00:53
1

You have to check all files that are loaded with all kinds of shells. For me, it turned out to be the /etc/bash.bashrc file which included these two lines

HISTSIZE=1000
HISTFILESIZE=2000

I can't really say why that mattered since I was resetting them in my .bashrc, but by commenting out these lines, I got it all working.

I started seeing this problem after switching to Ubuntu from Debian. Seeing as the lines weren't there on Debian, and that I never saw any problem there, I am quite confident that this is a final solution.

kalj
  • 111