Bash Configuration

This page is a collection of miscellaneous recommended configuration for Bash users, with full rationale given so you can decide whether they are appropriate for you.

History

Use a custom HISTFILE

Using the default history file makes it very easy to accidentally lose history. Setting HISTFILE to a nonstandard path protects you.

~/.bashrc (excerpt)
# Use a nonstandard history file to avoid accidentally losing history
HISTFILE=~/.bash_history_actual

Here are some of the ways you can lose history while using the default history file:

Each of these causes Bash to start with its default settings, and the default behavior is to immediately truncate the history file to 500 entries. By keeping your history in a nonstandard file, you ensure that Bash will only touch your history file when it has also loaded your other history settings. If you start Bash in a way that doesn’t load your settings, it will use the default history file, not your custom one.

To safely move your existing history file: First, close all terminals. Then, in a new terminal:

# Copy the existing history file to the new path
$ cp -i .bash_history .bash_history_actual
# Edit your .bashrc to add HISTFILE=~/.bash_history_actual
$ nano .bashrc

Then, verify that commands are being saved to the new file. In a new terminal:

# Run any easy-to-spot command
$ echo hello there
hello there
$ history -a
$ tail .bash_history_actual
# (Verify that echo hello there appears)

The original .bash_history can now be deleted or kept as a backup (but rename it to avoid confusing your future self).

$ mv .bash_history .bash_history.backup.$(date +%Y%m%d)

Save more history

Bash’s default is to store only 500 lines of history, which is tiny. In recent versions, both HISTSIZE and HISTFILESIZE can accept a negative value to indicate that their size should be unbounded. My strategy is to make these unbounded and revisit this decision if I start to notice performance problems later.

~/.bashrc (excerpt)
# Don't limit the size of the history file
HISTFILESIZE=-1
# Don't limit the size of the in-memory history list
HISTSIZE=-1

In older versions of Bash (I’m not sure how old), setting at least one of these variables to a negative value caused problems. I suggest checking man bash on your system to ensure that your version explicitly supports such values (search for the phrase “values less than zero” and verify that it appears for both variables).

If you don’t want to make these values unbounded, I suggest setting HISTFILESIZE to 500,000. Based on my personal experience, that should be enough to keep many years of history even with very heavy terminal use.

Readline

Readline is the library used by Bash (and some other programs) to provide command-line editing and completion features.

Improving readability

~/.inputrc (excerpt)
# Briefly highlight the corresponding opening symbol when entering a closing symbol
set blink-matching-paren on
# When listing possible completions, color the part that is already typed
set colored-completion-prefix on

Reducing keystrokes

Readline has two options that make tab completion more flexible. The first treats uppercase and lowercase as interchangeable for completion purposes, and the second treats underscores and hyphens as interchangeable. For example, with both options enabled, you can type a-bTab to complete a file that is actually named A_BASKETBALL.

~/.inputrc (excerpt)
set completion-ignore-case on
set completion-map-case on

You can also have Readline immediately print the possible completions when there are several options (instead of requiring a second Tab to print them).

~/.inputrc (excerpt)
set show-all-if-unmodified on