How can I modify the contents of my bash_history
file? What values or variables control how long the history lasts? Are there any other things I can change to provide finer control of my BASH history??

- 79,293
3 Answers
There are two variables that control the history size:
HISTFILESIZE The maximum number of lines contained in the history file. When this variable is assigned a value, the history file is truncated, if necessary, to contain no more than that number of lines by removing the oldest entries. The history file is also truncated to this size after writing it when a shell exits. If the value is 0, the history file is truncated to zero size. Non-numeric values and numeric values less than zero inhibit truncation. The shell sets the default value to the value of HISTSIZE after reading any startup files.
and
HISTSIZE The number of commands to remember in the command history (see HISTORY below). If the value is 0, commands are not saved in the history list. Numeric values less than zero result in every command being saved on the history list (there is no limit). The shell sets the default value to 500 after reading any startup files.
These two variables allow you to control the behavior of your history. Basically, HISTSIZE
is the number of commands saved during your current session and HISTFILESIZE
is the number of commands that will be remembered across sessions. So, for example:
$ echo $HISTSIZE
10
$ echo $HISTFILESIZE
5
$ history | wc
10 29 173
In the example above, because HISTSIZE
is set to 10, history
returns a list of 10 commands. However, if you log out and then log back in, history
will only return 5 commands because HISTFILESIZE
is set to 5. This is because, once you exit your session, your HISTFILESIZE
lines of your history are saved to your history file (~/.bash_history
by default but controlled by HISTFILE
). In other words, commands are added to HISTFILE
until that reaches $HISTFILESIZE
lines at which point, each subsequent line added means the first command of the file will be removed.
You can set the values of these variables in your ~/.profile
(or ~/.bash_profile
if that file exists). Do not set them in your ~/.bashrc
first because they have no business being set there and secondly because that would cause you to have different behavior in login vs non-login shells which can lead to other problems.
Other useful variables that allow you to fine tune the behavior of your history are:
HISTIGNORE
: This allows you to ignore certain common commands that are rarely of interest. For example, you could set:export HISTIGNORE="pwd:df:du"
That would cause any command starting with
pwd
,df
, ordu
to be ignored and not saved in your history.HISTCONTROL
: This one lets you choose how the history works. Personally, I set it toHISTCONTROL=ignoredups
which causes it to save duplicated commands only once. Other options areignorespace
to ignore commands starting with whitespace, anderasedups
which causes all previous lines matching the current line to be removed from the history list before that line is saved.ignoreboth
is shorthand for ignorespace and ignoredups.HISTTIMEFORMAT
: This allows you to set the time format of the history file. See Pandya's answer or readman bash
for details.
For further fine tuning, you have:
The
histappend
bash option. This can be set by runningshopt -s histappend
or adding that command to your~/.bashrc
. If this option is setthe history list is appended to the file named by the value of the HISTFILE variable when the shell exits, rather than overwriting the file.
This is very useful as it allows you to combine the histories of different sessions (think different terminals for example).
The
history
command has two useful options:history -a
: causes the last command to be written to the history file automaticallyhistory -r
: imports the history file into the current session.
You could, for example, add these two commands to your
PROMPT_COMMAND
(which is executed each time your shell shows the prompt, so whenever you start a new shell and after each command you run in it):export PROMPT_COMMAND='history -a;history -r;'
Combined, they ensure that any new terminal you open will immediately import the history of any other shell sessions. The result is a common history across all terminals/shell sessions.
Default size of the history file is 500 lines. Once the .bash_history file reaches 500 lines, the early entries get eliminated to make room for the newer lines, as in FIFO. You can change this by changing the value of the variable HISTFILESIZE
which by default has the value 500.
Putting a HISTFILESIZE=10000
in your .bashrc will increase the number of lines the history file can hold to 10000, thereby increasing the life of its contents.
-
2note: if you do raise
HISTSIZE
's limit, consider changingHISTFILE
too. If you runbash --norc
HISTSIZE
will go back to the default value, truncating yourHISTFILE
on exit. – llua Oct 21 '14 at 14:58 -
Thanks, @llua. Similarly, if your system's
/etc/bash.bashrc
sets aHISTFILESIZE
(or maybeHISTSIZE
; I haven't checked which), then Bash appears to truncate.bash_history
at the point it reads/etc/bash.bashrc
(or at least at some point before your new limit is set in your own Bash init files). To avoid this, again, set your ownHISTFILE
so that your real history goes there and not into the.bash_history
file that Bash is truncating. – Chris Povirk Nov 03 '17 at 12:18 -
(But don't
export
it, as doing so would make it visible to any child shells, which could then look to truncate it instead of.bash_history
!) – Chris Povirk Nov 03 '17 at 12:36
Read man bash
for more details covered about bash history like:
HISTCONTROL A colon-separated list of values controlling how commands are saved on the history list. If the list of values includes ignorespace, lines which begin with a space character are not saved in the history list. A value of ignoredups causes lines matching the previous history entry to not be saved. A value of ignoreboth is shorthand for ignorespace and ignoredups. A value of erasedups causes all previous lines matching the current line to be removed from the history list before that line is saved. Any value not in the above list is ignored. If HISTCONTROL is unset, or does not include a valid value, all lines read by the shell parser are saved on the history list, subject to the value of HISTIG‐ NORE. The second and subsequent lines of a multi-line compound command are not tested, and are added to the history regardless of the value of HISTCONTROL. HISTFILE The name of the file in which command history is saved (see HISTORY below). The default value is ~/.bash_history. If unset, the command history is not saved when a shell exits. HISTFILESIZE The maximum number of lines contained in the history file. When this variable is assigned a value, the history file is truncated, if necessary, to contain no more than that number of lines by removing the oldest entries. The history file is also truncated to this size after writing it when a shell exits. If the value is 0, the history file is truncated to zero size. Non-numeric values and numeric values less than zero inhibit truncation. The shell sets the default value to the value of HISTSIZE after reading any startup files. HISTIGNORE A colon-separated list of patterns used to decide which command lines should be saved on the history list. Each pattern is anchored at the beginning of the line and must match the complete line (no implicit `*' is appended). Each pattern is tested against the line after the checks specified by HISTCONTROL are applied. In addition to the normal shell pattern matching characters, `&' matches the previous history line. `&' may be escaped using a backslash; the backslash is removed before attempting a match. The second and subsequent lines of a multi-line compound command are not tested, and are added to the history regardless of the value of HISTIGNORE. HISTSIZE The number of commands to remember in the command history (see HISTORY below). If the value is 0, commands are not saved in the history list. Numeric values less than zero result in every command being saved on the history list (there is no limit). The shell sets the default value to 500 after reading any startup files. HISTTIMEFORMAT If this variable is set and not null, its value is used as a format string for strftime(3) to print the time stamp associated with each history entry displayed by the history builtin. If this variable is set, time stamps are written to the history file so they may be preserved across shell sessions. This uses the history comment character to distin‐ guish timestamps from other history lines.
Particularly HISTFILESIZE
and HISTSIZE
may you are looking for and helps you.

- 24,618
HISTSIZE
andHISTFILESIZE
are the only control surfaces that you have to control how much history is maintained. – slm Oct 21 '14 at 14:12