Exploring Linux Bash History

In a Linux server environment, you'll frequently interact with the terminal. Most Linux distributions come with the bash shell as the default, so you're likely to use it. During your command-line sessions, you'll find yourself repeatedly entering various commands, and often, you'll need to type slight variations of those commands even more frequently. 

Initially, manually entering each command can be helpful, but as you gain experience, it can become tiresome and disruptive. Fortunately, the bash shell offers robust history functions that keep track of all your terminal commands.

In this article, we explore what bash history is and where bash history is stored in your Linux system. We will also explore a useful history command that is used to manage the bash history.

What Is Bash History?

The “bash history” term refers to the record of previously executed commands in the bash shell, a popular command-line interface used in Unix-like operating systems. This history is a chronological list of the commands that you have entered in your terminal. The bash shell maintains this history to help users easily recall and reuse previously executed commands. 

In Linux, the “history” command is to list and view commands of your bash history. In this article, we will focus on the usage of the history command in Linux, as the term "bash history" is used to refer to this command. However, you can interact with bash history using configuration files (such as ~/.bash_history), expansions (e.g., !!), and keyboard shortcuts (e.g., the ↑ and ↓ keys) in addition to these built-in commands.

Where Is Bash History Stored?

In the bash shell, commands are initially stored in memory (RAM) as you run them inside your terminal, and these commands remain there until you log out of the terminal. After you log out, these commands are saved to a file named ".bash_history" in your Linux system’s home directory “/home/username/.bash_history.” This file maintains the record of your command history. Use the following command to find the bash_history file:

$ ls -l /home/username/.bash_history

It’s important to note that only a root user can view the command history of all users on your system. If you are a standard user, then you can only list your history logs.

You should also keep in mind that there are certain limitations on storing the number of commands. The memory-based history buffer can hold up to 1,000 command entries, while the disk-based history file can contain a maximum of 2,000 entries.

If you wish to modify how the "history" command functions in bash, you have the option to adjust it by making changes to environment variables, which we'll discuss in more detail shortly.

How to Use the Linux Bash History Command?

The history command is used to list the bash history in the Linux distribution. Therefore, to view the terminal history, use the history command without using any option:

$ history

The above command will display all terminal command history. 

You can limit the history command output. It means you can control the number of displayed entries in your command history by specifying a limit when using the "history" command. For example, if you want to see only the most recent ten entries, you can use the following command:

$ history 10

Search Bash History 

You can filter your command history by using grep or fewer commands with the "history" command by piping its output. For example, if you want to filter all commands that contain “Nginx,” use the following command:

$ history | grep nginx

Several keyboard shortcuts are available to search bash history. Here, we have listed:

Keyboard Arrows Keys:

  • ↑ keyAllows you to scroll in the backward direction through bash history.
  • ↓ keyAllows you to scroll in the forward direction through bash history.

Keyboard Alphabet Keys:

  • Pressing Ctrl+R allows you to search bash history commands.
  • The Ctrl+O can be used to execute a command that you found using the Ctrl+R search.
  • Ctrl+G is used to exit a Ctrl+R search.

To execute the most recent command that starts with a specific string, add this string after the exclamation point.


But if you don’t want to run the command directly above the command, you can use the option “p:”


The above command will now only search the recent command that starts with “sudo.” This command will not execute on your system directly.

To search for a command containing a specific string, but not necessarily at the beginning, you can add a question mark after the exclamation point. For example, to execute the most recent command that again includes "Nginx," you can use the following command:

$ !?search_string

$ !?nginx

Display Linux History with Timestamp 

You can change the history command output format by modifying the “.bashrc” file. All bash history settings are stored in this file that you can edit based on your requirements. For example, let’s say you want to display the timestamp with all commands in the bash history. Different timestamp formats are available that you can use with the bash history:

%d: Display Day

%m: Display Month

%y: Display Year

%H: To display Hour

%M: Display Minutes

%S: Display Seconds

%F: To display the full date format (Yy-Mm-Dd )

%T: To display in clock time (H: M:S format)

%c: To display in the complete date and timestamp format (Day-D-M-Y H: M:S)

To do this, open the “.bashrc” file using a source code editor. Here, we are using the default Nano editor:

$ sudo nano .bashrc

Add the following line to add a timestamp before each command in the bash history file:


Save the above changes using “Ctrl+o” in this file and press “Ctrl+x” to exit from this file. Now, execute the following command again to view changes in the bash history format:

$ history 

As you can see, now a timestamp is added before each command in the bash history file.

Change Bash History File and Buffer Size 

The file “.bashrc” contains two entries that control the bash history buffer size and file size.

HISTSIZE: Used to display bash history buffer size. It contains the maximum number of entries for the history list.

HISTFILESIZE: The maximum size of the bash history file.

You can easily change both of the above environment variables in the .bashrc file.

Exclude Commands to Record in Linux Bash History

To omit specific commands from being recorded in your bash history through the use of the “~/.bashrc” file, you can make use of the $HISTIGNORE environment variable. 

By setting $HISTIGNORE, you can specify a list of command patterns separated by colons that won't be stored in your bash history. For instance, if you want to exclude commands involving "sudo" or any command beginning with "ls," you can include these patterns in your ~/.bashrc file.

HISTIGNORE='sudo *':'ls *'

Save changes and exit from this file. Now, the sudo and ls commands will not be recorded in your bash history file.

How to Delete Bash History?

Often, there are cases when you don't want other users on your Linux system to access your terminal history. For instance, when teaching a class or sharing a computer, you might have demonstrated potentially harmful commands. Some students, out of curiosity, may experiment with these commands, leading to system issues. While system recovery is possible, it's not ideal. It's best to clear your command line history to maintain privacy and security, especially when sharing access with others. 

Deleting a specific line from your Linux bash history file can be accomplished using the "history" command with the -d option. Just specify the line number you want to remove. For instance, if you need to erase a command that contains sensitive information like a password entered in plain text, you can identify the line number in your history and execute the command like this:

$ history -d 38

To clear or delete all entries in your B=bash history, you can utilize the -c option with the "history" command. Running the following command will remove all entries from the history:

$ history -c

Alternatively, if you wish to permanently delete all records of previously executed commands in the bash history file, you can use the following command:

$ cat /dev/null > ~/.bash_history


In this article, we have demonstrated the use of the history command in Linux. The bash history is a file or repository that keeps the record of all commands executed by a user in a Linux command-line environment. This bash history feature allows users to easily find and reuse previously executed commands on their Linux system, often with the convenience of arrow key navigation.

If you are using a VPS server, the history command will help you find and execute desired commands on your servers efficiently. Please let us know if you find any problems related to this article. Thanks!

Blog company: