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.

Previous:Find Current Folder Name in Bash

Next: None

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

Find Current Folder Name in Bash

Publish Date:2025/03/22 Views:106 Category:OPERATING SYSTEM

Finding a directory is very easy through Bash scripting. But finding the exact directory folder name you are in right now is a bit complicated. This article will introduce three methods to find the folder name from this article directory. I

Changing Directories in Bash

Publish Date:2025/03/22 Views:75 Category:OPERATING SYSTEM

In this article, we will learn how to change directories in Bash. The term directory is used to refer to a folder. Because you frequently move between folders when using Bash and when using the Git version control system, it is essential to

Running Regular Expressions in Case Statements in Bash

Publish Date:2025/03/22 Views:75 Category:OPERATING SYSTEM

This article explores regular expressions, their basic syntax, and how to run them in Bash using case and if-else constructs. Introduction to Regular Expressions A regular expression, also called regex or regexp, is a sequence of characters

Sending a message to logged in users in Bash

Publish Date:2025/03/22 Views:168 Category:OPERATING SYSTEM

This article explores methods to send data to another logged in user in Bash. This article discusses methods to find active users and send them messages. Finding Online Users in Bash Before sending data to an online user, you must verify wh

Bash copy all files from remote directory

Publish Date:2025/03/22 Views:156 Category:OPERATING SYSTEM

This article describes methods for copying files between different hosts on a network. It also explores the scp command and how to use it to copy files from a remote location. scp command Secure Copy Protocol (SCP) is a secure copy network

Batch file to loop through files in subdirectories

Publish Date:2025/03/22 Views:96 Category:OPERATING SYSTEM

This article explains how we can write a batch script to loop through files in subdirectories. We will take an example to explain the concept. Batch file to loop through files in subdirectories Assume we have the directory structure shown b

Batch file to remove X characters from a file name

Publish Date:2025/03/22 Views:153 Category:OPERATING SYSTEM

This article explains how we can remove specific characters from the filename of a file using a batch script. We will cover several methods below to rename files on Windows. File Explorer Renaming on Windows File Explorer offers the least f

Declaring variables in batch script

Publish Date:2025/03/22 Views:65 Category:OPERATING SYSTEM

This article will demonstrate how to declare and define variables in a batch script. Declaring variables in batch script In Batch, you do not need to use any additional keywords to declare integer, float, double, or string type variables. T

Batch Check for Empty Variables

Publish Date:2025/03/22 Views:86 Category:OPERATING SYSTEM

This article explains how we can test if a variable has been set. We can use an if statement to check if a variable has been defined. Batch check whether variables are empty For a simpler context, we will start by defining two strings, str1

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial