1. Overview

Whenever we work on any Linux terminal, more often than not, we use the Bash shell. We type many common commands multiple times with different options. Sometimes while typing a verbose command, we realize that there is some mistake. Instead of retyping the whole text, we use rich history features to correct and search.

In this tutorial, we’ll see how to use Bash history to work more efficiently.

2. Basic Configuration to Enable History

For the Bash shell to allow the history operation, we first need to enable history. We also need to configure the Readline library to read the input from the command line. In this section, we’ll discuss how to enable history and configure the Readline library.

When the Bash shell starts, it initializes history and reads the .inputrc file for the Readline library configuration.

2.1. Enabling Bash History

First, let’s check if history is enabled or not:

$ set -o | grep history
history            on

In case history is turned off, we’ll turn it on by setting in the .bashrc file:

$ set -o history

After that, let’s find out whether the file location and history size are set or not by using the echo command. The HISTFILE variable stores the location of the history file:

$ echo $HISTFILE

If the value is /dev/null, then as always, we need to set it up in our .bashrc file:

HISTFILE=$HOME/.bash_history

We use the HISTSIZE and HISTFILESIZE variables to configure the length of the Bash history:

$ echo $HISTSIZE
$ echo $HISTFILESIZE

If the value of either HISTSIZE or HISTFILESIZE is zero, we need to configure them in the .bashrc file:

HISTFILESIZE=500
HISTSIZE=500

2.2. Configuring the Readline Library

Now, we’ll configure the Readline Library. For this purpose, we need to work with the .inputrc file. The .inputrc file describes key bindings, variable assignment, and conditional syntax. We can override the default behavior of the Readline library by customizing the .inputrc file in the home directory.

For changing the editor from emacs (default mode) to vi:

$ set editing-mode vi

To set the visible bell:

$ set bell-style visible

In case we want to ignore the case:

$ set completion-ignore-case On