Apple - Can't increase Mac OSX bash shell history length

You also need to set HISTSIZE=10000000.

HISTFILESIZE determines the number of lines to keep in the history file while HISTSIZE determines the number of lines to keep in memory. Both default to 500

Execute echo $HISTFILESIZE $HISTSIZE before you make the change and then again after. You should see 10000000 500 first then 10000000 10000000 after.


You need to export HISTSIZE and HISTFILESIZE environment variables in your ~/.bash_profile file.

Add following lines in your ~/.bash_profile:

# history size
export HISTFILESIZE=1000000
export HISTSIZE=1000000

Answer:

All you need to do is set HISTSIZE in your bash profile (~/.bash_profile). You can leave HISTFILESIZE unset. This will configure both the in-memory history and the file-based history.

# Saves 10 million lines in memory and in the bash history file.
HISTSIZE=10000000

Explanation, additional information.

The problem is HISTFILESIZE only sets the maximum history stored to file when you startup a session. HISTSIZE is what determines how many lines get saved at the end of a session. If HISTFILESIZE is larger than your HISTSIZE, you'll never see more than your HISTSIZE because the file is getting overwritten with HISTSIZE commands (unless you set history to append, but that is a separate topic: look up histappend in shell builtin commands for that).

This is the reason you're seeing no effect. With HISTSIZE at default (probably 500) your history file is always getting overwritten by the most recent $HISTSIZE commands. Anything older (which could potentially grow the list past $HISTSIZE) is wiped away.

For most cases there's no need to set HISTFILESIZE independently of HISTSIZE.

HISTSIZE determines how many lines will get written to the history file. The command list is truncated upon exit, before writing to the history file, keeping N most recent lines. By default, this list overwrites the history file.

HISTFILESIZE determines how large the history file (in lines) can be. The file gets truncated upon startup, keeping N most recent lines. In addition, the history file is potentially truncated whenever HISTFILESIZE is set.

Finally, although it appears to be undocumented, BASH sets HISTFILESIZE based on HISTSIZE if HISTSIZE is set during startup. I've tried this out on macOS, and a few varieties of Ubuntu, and the behavior is consistent. If you set HISTSIZE in a launch file (not via the command line by any means), HISTFILESIZE will be set to match. (Try it yourself: set HISTSIZE to some arbitarily large value in .bash_profile, exit and start a new terminal, then echo $HISTSIZE $HISTFILESIZE and you'll find them matching.)

As a result, there's no reason to touch HISTFILESIZE unless you want different values, and the uses cases to have different values are rare (and typically involve more history customization settings to get something useful).

Tags:

Macos

Bash