Customizing and Utilizing History in the Shell

Every command that is typed into a Linux system’s terminal is typically stored in the history buffer for a period of time. This explains why pressing the up arrow in terminal will let you cycle through past commands that have been executed. This buffer of past commands can be customized according to an administrator’s needs, allowing us to vary the number of saved commands, clear the current history, or change other settings.

There are also a handful of helpful ways that the command history can be utilized. Such methods include the ability to recall past commands with only a few keystrokes, or search through the history buffer for a command you might have forgotten how to execute. In this tutorial, we will elaborate on the command history for a Linux system, and look at different ways to customize and utilize the buffer to increase productivity.

In this tutorial you will learn:

  • How does command history work?
  • Where is the shell command history stored?
  • How to view a user’s command history
  • How to execute a recent command
  • How to search the history buffer for commands
  • How to control the size of the history buffer
  • How to avoid recording a command in history buffer
  • How to clear history buffer
  • How to use Bang commands
Customizing and Utilizing History in the Shell
Customizing and Utilizing History in the Shell
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Introduction to Command History

When a command is executed in the shell, they are kept track of in the system’s history buffer. It helps to think of it like a web browser, which keeps track of the websites we visit, and makes it easier to access them again in the future. Same concept.

The history buffer is usually kept inside of the .bash_history file inside of each user’s home directory. This file contains a certain number of all recent commands that the user has executed in terminal. You can see it for yourself by using cat to view its contents:

$ cat ~/.bash_history

The history command is another way to see the contents of the file.

$ history
Viewing history of executed commands in Linux shell
Viewing history of executed commands in Linux shell

As seen in the screenshot above, the output of history shows all recently executed commands, in the order that they were executed, as a numbered list.

Command History Functions

Let’s look at a few examples of how we can take advantage of the command history buffer for convenience and to enhance our workflow.

  1. Specify a number after the history command in order to see a certain number of recent commands. For example, if you want to see the five mostly recently executed commands:
    $ history 5

    This is helpful when you need to recall a recent command that should not be too far up in the buffer. Without specifying a number, too many results will be returned and the output is less helpful.

  2. Once you have spotted a command from the history list that you with to execute again, you can simply enter an exclamation point ! followed by the command’s corresponding number. For example, if you view the history buffer and notice that sudo apt update is command number 11 on the list, and want to reexecute this command, then we would use the following syntax:

    $ !11

    This saves us a lot of keystrokes and we can quickly recall the recent command.

    We managed to reexecute a command from the history buffer
    We managed to reexecute a command from the history buffer
  3. While in terminal, use key combination Ctrl + R to initiate a search for a recently executed command. Just start typing some part of the command that you can remember, and the history buffer will automatically be searched for the closest matching result. For example, here we searched for update in order to find the sudo apt update command, which had been previously executed:
    $ [Ctrl + R] update
    Searching for a recently executed command in Linux
    Searching for a recently executed command in Linux
  4. Perhaps an even faster way to recall a command is with an exclamation point ! and specifying the first part of the command. For example, if we know that a recent command began with sudo, we can type !sudo into the terminal to recall it instantly.
    $ !sudo

    The screenshot below shows how we were able to execute sudo apt update by only typing !sudo, since it is the most recently executed command that begin with that key word.

    Executing a recent command by specifying the first few letters of it
    Executing a recent command by specifying the first few letters of it
  5. Bang commands can be used to reexecute a command very quickly. Let’s look at a few of them. First, to execute the last command again, we use double exclamation points:
    $ !!

    To get the last operand of the previous command, we can use !$. In the case of our sudo apt update example, we could specify the command like so:

    $ sudo apt !$

    This would fill in update since it is the last operand of the most recently executed command.

  6. What if we are typing a command and we do not want it to be recorded? This privacy measure has already been considered, and there is an easy way to avoid the history buffer picking up on one of our commands. Simply put a space before the command you are executing, and it will not be recorded:
    $  df -h

    Notice the preceding space before our df -h command. This prevents it from showing up in the history buffer.

  7. In case you want to clear the entire history buffer, the -c option can be used with the history command.
    $ history -c

    Alternatively, or just for extra measure, you can manually clear out the ~/.bash_history file of all content:

    $ echo "" > ~/.bash_history
  8. In case you need to export the command history, just redirect the output of the history command to a file:
    $ history > command_history.txt

    Or to only get a certain number of recent commands:

    $ history 10 > last_10_commands.txt

Control the Size of History Buffer

There are two environment variables that control the size of the history buffer in Linux. These are HISTSIZE and HISTFILESIZE.

$ echo $HISTSIZE

The first variable HISTSIZE specifies how many recent commands should be remembered. In our case, it is 1000. The second variable specifies how big the history buffer file (.bash_history) can be – 2000 in our case.

The easiest way to change these settings and make them persist across future sessions is to edit the user’s .bashrc file.

$ nano ~/.bashrc

At the bottom of this file, paste the following two lines:

export HISTSIZE=1000
export HISTFILESIZE=2000

Of course, change the values to your desired settings. Then, save changes to the file and exit it. To make changes take effect in the current session, execute:

$ source ~/.bashrc

Closing Thoughts

In this tutorial, we saw how to customize and utilize the history buffer on a Linux system. It is easy to leverage the history feature to recall recent commands and enhance our workflow in the terminal, thanks to intuitive features with how the history buffer is implemented. In minimal keystrokes, we can reexecute old commands or search for the elusive one that we have forgotten.

Comments and Discussions
Linux Forum