Using and Customizing Bash Command History

29/12/2020
The bash shell is the default command line environment available in most Linux distributions. Similar to all shell environments, it keeps a record of commands that have been executed previously by the user. This record is kept and maintained even we restart our system.

You might know the basic use of history command, but it can do a lot more than that. Bash history is usually stored in the file ~/.bash_history. It enables you to recall and reuse the stored record in an efficient way to get the best out of bash history saving feature. Not only this, but you can also customize and control the bash command output in the way you want.

In this article, we will explain how to effectively use and customize the bash command history to get the most out of its features.

We have used Debian 10 for running the commands and procedure mentioned in this article.

Using bash command history

1. Viewing the bash History

To view the entire history of shell commands, you can run the following command in Terminal:

$ history

It will list the entire history for a specific user from the history file stored specifically for that user. You will see all the commands starting with a number allocated to each of them. It will list the older commands at the top starting with number 1 and the newer commands at the bottom.

2. Searching the History Output

You can also search for a specific keyword from the history output. Pair the history command with grep and a specific keyword to search for commands that match your specified keyword as follows:

$ history | grep [keyword]

For instance, to list all the commands that include the keyword “find”, the command would be:

$ history | grep find

3. Viewing last n commands

The history command by default lists the last 1000 number of commands executed by a user. In case, you want to list only a specific number let’s say n number of last executed command, run the following command in Terminal:

$ history n

For instance, to list the last 7 executed commands, the command would be:

$ history 7

To view the number of the last n run commands that includes a specific keyword, you can use the following syntax:

$ history | grep keyword |tail -n

An example of this would be to view the last 4 executed commands with the keyword “java”.

$ history | grep java |tail -n

4. Viewing oldest commands

To view the oldest n number of commands, you can use the following syntax in Terminal:

$ history | head -n

To view the oldest n number of commands that includes a specific keyword, use the following syntax:

$ history | grep keyword |head -n

An example of this would be to view the oldest 4 executed commands with the keyword “java”.

$ history | grep java |head -4

5. Clear Bash history completely

To remove the entire bash history, run the following command in Terminal:

$ history -cw

Customizing bash command history

To customize bash command history, we will have to make changes in the ~/.bashrc file. To edit the ~/.bashrc file, use the following command:

$ nano ~/.bashrc

Once you are done with modifying the file, use Ctrl+O and Ctrl+X to save and close the nano editor.

Then run the following command to apply the modifications:

$ source ~/.bashrc

1. Add Date and Timestamp to Bash History

If you want to display date and timestamp along with the command history, you can do that by adding the following line in ~/.bashrc:

$ export HISTTIMEFORMAT=‘%F, %T ‘

Now run the history command and it will show the command history with corresponding data and timestamp.

2. Increasing size of Bash History

Bash by default keeps 500 commands in the history list. However, we can change this value using the HISTSIZE value.

To view the current size of bash history, run the following command in Terminal:

$ echo $HISTSIZE

Similarly, the default size of the bash history file is 500. It is the maximum number of entries that are contained in the history file.

To increase the size of bash history let’s say 10000, add the following lines in  ~/.bashrc file:

$ HISTSIZE=10000
$ HISTFILESIZE=10000

To verify if the bash history size has changed successfully, run the following commands in Terminal:

$ echo $HISTSIZE
$ echo $HISTFILESIZE

3. Append Bash Commands to History File

When a bash session is closed, you can choose to whether overwrite or append the commands in the history file using the histappend variable. To view the current settings, run the following command in Terminal:

$ shopt histappend

The “on” in the output shows histappend option is enabled and the commands will be appended in the history file instead of overwriting. While “off” shows, histappend option is disabled and the file will be overwritten.

Open the ~/.bashrc file and:

Add following line, if you want to append the commands to the history file instead of overwriting:

$ shopt -s histappend

Or add the following line, if you want to disable the append option and want to overwrite the file on exit:

$ shopt -u histappend

4. Store Bash History Immediately

Bash by default only saves the session to the bash history file once the session ends. To change this default behavior and make it to instantly save each command you have executed, you can make use of PROMPT_COMMAND.

Edit the ~/.bashrc file and add the following line:

$ PROMPT_COMMAND=‘history -a’

Now whenever you execute any command, it will be immediately added to the history file.

5. Control Bash History

We can control the way bash saves our command history through the HISTCONTROL variable. We can specify it to ignore duplicate entries, and/or to ignore entries with leading white spaces.

  • ignorespace – eliminates commands that begin with a space history list.
  • ignoredups – eliminate duplicate commands.
  • ignoreboth – Enable both ignoredups and ignorespace
  • erasedups- eliminate duplicates from the whole list

To apply these functions, open the ~/.bashrc and add the following line with values separated by the colon as follows:

$ export HISTCONTROL=ignorespace:ignoredups

6. Ignore Specific Commands

We can also control which commands to ignore in history using a variable HISTIGNORE. It is a colon-separated list of patterns in which we can specify all the commands that we want to ignore from history.

For instance, if we do not want to list the basic commands such as history, ls, pwd commands in the history list, then we add the following line in the ~/.bashrc  file:

$ export HISTIGNORE="history:ls:pwd:"

With Linux bash command history, you can do a lot more than just repeating the old commands. In this article, we have learned how to use the bash history to view the commands that have executed previously and also learned to control the way bash saving the command history.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Bash declare command

Bash doesn’t have a strong type system. To allow type-like behavior, it uses attributes that can be set by a command....
29/12/2020

Bash Parameter Expansion

The parameter is used in bash to store data. Different types of data can be stored in the parameter, such as integer, string,...
29/12/2020

Bash uniq Command

Linux users need to create or read the text file in regular basis for many purposes. A text file can contain different...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024

Dịch vụ thuê mua proxy giá rẻ an toàn, tốc độ cao
13/05/2024

Thuê mua proxy V6 uy tín, chất lượng tại đâu?
11/05/2024

Thuê mua proxy Tiktok tăng doanh thu, hiệu quả cao
11/05/2024