📒 

The history Command in Linux (Bash History)

The history command in Linux is a powerful tool for users working with the Bash shell. It keeps track of previously executed commands, allowing users to view, recall, and even re-execute commands without typing them out again. This can significantly boost productivity by saving time and reducing the chance of making mistakes when retyping long commands.

What is Bash History?

Bash, like many other shells, maintains a record of commands you enter in a history file. In most Linux distributions, this history is stored in a file called .bash_history in each user’s home directory (~/.bash_history). By default, each command entered in the shell gets appended to this file when the session ends, allowing users to revisit commands from previous sessions.

Basic Usage of the history Command

The history command is simple to use and provides various ways to view and interact with your command history. Here are some of the most common uses:

  • Display Command History:
    Running the history command without any arguments will display a list of the most recent commands you’ve entered, along with a number next to each command.

    history

    This displays the last n commands, where n is typically around 500 or 1000 depending on your system’s configuration.

  • Specify the Number of Commands:
    You can limit the number of commands shown by specifying a number. For example, to see the last 10 commands:

    history 10

Recalling Commands from History

The history command also allows you to easily recall and reuse previously executed commands:

  • Using the ! (Exclamation Mark) Notation:
    This allows you to re-execute commands directly from the history:

    • !! – Re-runs the last command you executed.
      !!
    • !n – Runs the command at position n in the history list.
      !42

      This would re-execute the command listed at number 42 in the history.

    • !string – Finds the most recent command that starts with string and executes it.
      !git

      This would execute the most recent command starting with git.

  • Using the Arrow Keys:
    You can use the up and down arrow keys to scroll through previous commands directly in the terminal. This is especially useful for quickly finding recent commands.

Editing and Modifying History

  • Remove Specific Entries:
    You can delete a specific command from the history by using history -d followed by the line number:

    history -d 42

    This would delete the command at line 42 in the history list.

  • Clear Entire History:
    To remove all commands from the history file, use:

    history -c

    This clears the history for the current session. To also clear the saved history in ~/.bash_history, overwrite the file:

    history -c && > ~/.bash_history

Configuring Bash History

The behavior of history can be customized through environment variables and configuration settings in the ~/.bashrc or ~/.bash_profile files:

  • HISTSIZE:
    This variable controls how many commands are kept in memory during a session. For example, to keep 1000 commands:

    export HISTSIZE=1000
  • HISTFILESIZE:
    This variable sets the number of lines (commands) that are saved in the ~/.bash_history file. For example:

    export HISTFILESIZE=2000

    This means that 2000 commands will be kept in the history file even after logging out.

  • HISTCONTROL:
    This variable determines how commands are stored. Common options include:

    • ignoredups – Prevents duplicate commands from being added to history.
    • ignorespace – Commands that start with a space are not added to history.
    • ignoreboth – Combines ignoredups and ignorespace.

    Example of setting HISTCONTROL to ignore duplicates:

    export HISTCONTROL=ignoredups
  • HISTTIMEFORMAT:
    To include a timestamp with each history entry, set HISTTIMEFORMAT:

    export HISTTIMEFORMAT=”%F %T “

    This will prepend each history entry with the date (%F) and time (%T).

Searching Through History

  • Reverse Search with Ctrl + r:
    One of the most efficient ways to search for a command in your history is using Ctrl + r. Start typing part of the command, and Bash will find matching commands as you type. Pressing Ctrl + r again will cycle through previous matches.
  • Grep with history:
    You can also use grep to search through the output of history:

    history | grep git

    This will show all commands that contain the word git.

Conclusion

The history command is a fundamental part of working with the Bash shell, offering a way to keep track of past commands and streamline workflows. With its ability to recall, search, and customize command history, it helps make command-line operations more efficient and user-friendly. By mastering history and its related features, users can significantly speed up their work in Linux environments.