JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Bash History Size

Author:JIYIK Last Updated:2025/03/22 Views:

In this article, we will learn about Bash history, its size, and how we can change our history size and handle limits. Before getting into our topic, let us first understand why we need history in Bash shell and how we can get it.

Most developers, system administrators, and software application engineers spend a lot of time on the Linux command line. Being human, they make typing mistakes.

Furthermore, many commands have a list of arguments/options (and their respective order), so it can be hard to remember them in their exact order.

Thus, history helps us recall the commands we have previously typed in the Bash shell.


Bash History Overview

In terms of history, Bash keeps track of commands, files, and shortcuts. The user can type on the command line using the history utility.

The History utility provides useful information. It helps to trace how the current system or account changes may have occurred.

Bash has two built-in commands related to Bash history:

  1. history - This command provides a list of previously used commands in your Bash history. It also allows you to modify your Bash history.
  2. fc - This command performs all the tasks of the history command and allows the command to be executed.

All information about Bash history will be stored ~/.bash_historyin the file .history for the Bash user, while for other users it may just be .history.

Let's start with the history command and see how it works.


Use the Bash history command to view Bash history

If you want to see your entire history, you can write history command in shell. It will list the entire history in history file.

$ history

The output will look like this:

    1  ping baidu.com
    2  sudo apt update
    3  ping baidu.com
    4  java --version
    5  sudo apt install openjdk-17-jre-headless
    6  sudo apt-get install gcc
    7  sudo apt-get install gpp
    8  sudo apt-get install g++
    9  sudo apt-get install mingw-w64
   10  sudo apt-get install codeblocks
   11  codeblocks
   12  sudo apt update
   13  sudo apt update mu-editor
   14  sudo apt install mu-editor
   15  sudo apt update
   16  python3 --version
   17  clear
   18  snap find pycharm
   19  sudo snap install pycharm-professional --classic
   20  clear
   21  cd desktop/python
   22  cd desktop/python task5.py
    ...

There are other useful Bash history commands which we will not explain, but you can read about them from this website.

The command in Bash in Linux fcis also called fixthe command. It is mainly used to modify multi-line commands.

The -l parameter of the fc command displays the previous command history.

Let's look at the following example of a command and its output:

$ fc -l
302     284 ./a.out
303     285 gcc execve1.c -o e1
304     286 gcc execve2.c
305     287 ./a.out
306     288 $ sudo snap install whatsdesk
307     289 sudo snap install whatsdesk
308     290 cd ..
309     291 cd CodeForces/
310     292 pwd
311     293 history
312     294 1  ping google.com
313     295 ls
314     296 wc Test.c
315     297 fc -l
316     298 ls
317     clear

Bash history size

If you are talking about the Bash history file, you assume there is some data there as well. The default value for the Bash history file is 500 to 1000 entries.

It is the maximum number of entries in the history file. But it is not permanent as we can configure the history size to what we want using different commands.

Let’s explore.

Configuring Bash History

To configure Bash command history, you must change the ~/.bashrc file. You can update the ~/.bashrc file using the following nano command :

$ nano ~/.bashrc

After making the changes, press Ctrl+O and then Ctrl+X to save and close the nano editor. Then, run the following command to reload your Bash settings:

$ source ~/.bashrc

or

$ . ~/.bashrc

Both commands will modify the Bash history file.


Modify the Bash history file size

The variables HISTSIZE and HISTFILESIZE are related to Bash history. The variable HISTSIZE has a count of the largest Bash command to be cached, while the variable HISTFILESIZE has a count of the top-level commands stored in the history file.

By default, we can keep up to 500 commands in the history file.

We can see these values ​​by printing these variables; the output may vary on different machines/configurations. Here is an example run and the associated results:

$ echo $HISTSIZE
$ echo $HISTFILESIZE
1000
2000

We can configure these values ​​by adding the following lines to the file ~/.bashrc :

$ HISTSIZE = 6000
$ HITFILESIZE = 6000

Again, we can print these variables to check the values:

$ echo $HISTSIZE
$ echo $HISTFILESIZE
6000
6000

Setting unlimited Bash history file size

The previous section discussed ways to modify the history size in Bash. However, sometimes it would be convenient to set it to an unlimited size. How would we do that? Let’s explore together!

You can also set the Bash history size or total file size using the following steps:

  • Change the Bash history file. As you can see in StackOverFlow, there are some issues with the Bash history file because it gets truncated in certain situations.

    We can fix this by changing the HISTFILE environment variable to use a different directory.

    export HITSFILE= ~/.history
    
    HISTFILE is the path/location of the history file (default is ~/.bash_history).
  • Increase the history size. Set the HISTFILESIZE and HISTSIZE variables to the empty string to make the Bash history size unlimited.
    $ export HITSFILESIZE=
    $ export HITSIZE=
    
  • Copy the existing history. Although we are using a new history file, we should not lose the previous file commands, so we copy the contents of the old file into the new one.
    $ cp ~/.bash_history ~/.history
    

Controlling Bash History

You can exclude commands from the Bash history to avoid output noise or for security reasons. You can do this using the history control command HISTCONTROL, which controls the history that is stored.

You can specify it to ignore duplicate entries and entries with leading spaces in your ~/.bashrc file.

  • ignorespace – This eliminates commands that begin with a space from the history list.
  • ignoredups – This eliminates duplicate commands.
  • ignoreboth – This enables both ignoreups and ignorespace.
  • erasedups - This removes duplicates from the entire list.
$ export HISTCONTROL=ignorespace:ignoredups

You can also control the commands to be ignored using the variable HISTIGNORE. It is a colon-separated list of patterns in which we can specify all the commands we want to omit from the history.

lsFor example, if you want to ignore basic and most frequently used commands (such as and pwd) from your history list , write the following line in your ~/.bashrc file:

$ export HISTIGNORE="ls:pwd:"

You can do more with the Linux Bash command history than just repeating old commands.


Save history to HISTFILE before logging out

These commands are not written to the Bash file until the user logs out. However, the write option can be used with the history command to write the history to the file before logging out.

The syntax is:

$ history -w

This command will write all current session command history to HISTFILE. After this, the command script will be available for future use.

Finally, in this article, we covered Bash history, history size, and file size, or how we can modify Bash history and its size to limited or unlimited range.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

How to decompress x.tar.xz format files under Linux

Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM

A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr

Summary of vim common commands

Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM

In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme

Detailed explanation of command return value $? in Linux

Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM

? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re

Common judgment formulas for Linux script shell

Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM

In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –

How to use the Linux file remote copy command scp

Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM

Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial