You’d like more control over your command-line history.
Set the $HIST* variables and shell options as desired.
The $HISTFILESIZE variable sets the number of lines permitted in the $HISTFILE. T
he default for $HISTSIZE is 500 lines, and $HISTFILE is ~/.bash_history unless you are in
POSIX mode, in which case it’s ~/.sh_history. Increasing $HISTSIZE may be useful, and unsetting it causes the $HISTFILE length to be unlimited.
Changing $HISTFILE probably isn’t necessary, except that if it is not set or the file is not writable, no history will be written to disk.
The $HISTSIZE variable sets the number of lines permitted in the history stack in memory.
$HISTIGNORE and $HISTCONTROL control what goes into your history in the first place.
$HISTIGNORE is more flexible since it allows you to specify patterns to decide what command lines to save to the history. $HISTCONTROL is more limited in that it supports only the few keywords listed here (any other value is ignored):
Command lines that begin with a space character are not saved in the history list.
Command lines that match the previous history entry are not saved in the history list.
Shorthand for both ignorespace and ignoredups.
All previous command lines that match the current line are removed from the history list before that line is saved.
If $HISTCONTROL is not set, or does not contain any of these keywords, all commands are saved to the history list, subject to processing $HISTIGNORE.
The second and subsequent lines of a multiline compound command are not tested, and are added to the history regardless of the value of $HISTCONTROL.
(Material in the preceding paragraphs has been adapted from Edition 2.5b of The GNU Bash Reference Manual for bash Version 2.05b, last updated July 15, 2002; http://www.gnu.org/software/bash/manual/bashref.html.)
As of bash version 3, there is a fascinating new variable called $HISTTIMEFORMAT.
If set and non-null, it specifies an strftime format string to use when displaying or writing the history.
If you don’t have bash version 3, but you do use a terminal with a scrollback buffer, adding a date and time stamp to your prompt can also be very helpful.
Watch out because stock bash does not put a trailing space after the format, but some systems (e.g., Debian) have patched it to do so:
1 ls -la
2 help history
3 help fc
bash-3.00# export HISTTIMEFORMAT='%Y-%m-%d_%H:%M:%S'
1 2006-10-25_20:48:04ls -la
2 2006-10-25_20:48:11help history
3 2006-10-25_20:48:14help fc
5 2006-10-25_20:48:39export HISTTIMEFORMAT='%Y-%m-%d_%H:%M:%S'
bash-3.00# HISTTIMEFORMAT='%Y-%m-%d_%H:%M:%S; '
1 2006-10-25_20:48:04; ls -la
2 2006-10-25_20:48:11; help history
3 2006-10-25_20:48:14; help fc
4 2006-10-25_20:48:18; history
5 2006-10-25_20:48:39; export HISTTIMEFORMAT='%Y-%m-%d_%H:%M:%S'
6 2006-10-25_20:48:41; history
7 2006-10-25_20:48:47; HISTTIMEFORMAT='%Y-%m-%d_%H:%M:%S; '
8 2006-10-25_20:48:48; history
# Getting tricky now
bash-3.00# HISTTIMEFORMAT=': %Y-%m-%d_%H:%M:%S; '
1 : 2006-10-25_20:48:04; ls -la
2 : 2006-10-25_20:48:11; help history
3 : 2006-10-25_20:48:14; help fc
4 : 2006-10-25_20:48:18; history
5 : 2006-10-25_20:48:39; export HISTTIMEFORMAT='%Y-%m-%d_%H:%M:%S'
6 : 2006-10-25_20:48:41; history
7 : 2006-10-25_20:48:47; HISTTIMEFORMAT='%Y-%m-%d_%H:%M:%S; '
8 : 2006-10-25_20:48:48; history
The last example uses the : built-in with the ; metacharacter to encapsulate the date stamp into a “do nothing” command (e.g., : 2006-10-25_20:48:48;).
This allows you to reuse a literal line from the history file without having to bother parsing out the
Note the space after the : is required. There are also shell options to configure history-file handling.
If histappend is set, the shell appends to the history file; otherwise it overwrites the history file.
Note that it is still truncated to $HISTSIZE. If cmdhist is set, multiline commands are saved as a single line, with semicolons added as needed.
If lithist is set, multiline commands are saved with embedded newlines.