Playing With Your Bash History

Your Linux computer, assuming it’s ‘normal’ will retain the commands you typed in the terminal and this is your Bash history. Like all things Linux, this is something you can control. The ability to customize these things is one of the things that makes Linux great!

More specifically, this article will cover how to change the Bash history size. That’s one of the things you can control. It’s one of the settings you can customize. You don’t have to accept the default Bash history size. It’s easy enough to change those settings to whatever you want.

I’m going to assume ‘normal’. That is, normal to me. I’m going to avoid the word ‘standard’ because I don’t know if these things that are normal to me are standards. For example, even the Bash shell isn’t a standard. However, it’s normal for a Linux distro to provide Bash – typically as the default shell. It’s not a standard, it’s just normal. That is, normal to me.

Got it?

Well, your Linux computer usually uses Bash shell. It also normally keeps a history of commands you type. This file is normally a hidden file, called .bash_history and is a plain text file. 

Bash has a control file. That is, Bash has various settings that can be configured. This file is also normally hidden. It’s usually called .bashrc. As this is about the shell and Bash, you will need an open terminal to follow along with this article. You can normally just press CTRL + ALT + T to open a terminal.

What is the Bash shell?

Bash is a Unix shell. It stands for Bourne Again Shell. It has long since been ported to Linux and is what you’ll normally find when you look into the more popular Linux distros.

A shell is a command line interpreter. It’s a user interface that lets you interact with your computer in a text-based manner. There are other shells besides Bash, but this article isn’t going to cover any of them.

This article presumes you’re using the default Bash shell. If you’re curious, here’s a big comparison of shells. Some people like to use different shells with csh and and zsh being popular choices. 

Bash is just one of many possible shells, but it’s normal to see Bash as the default. If you’re not using Bash, this article may not apply. You won’t see me writing much about other shells, as I’ve mostly just used the default shell rather than spending time exploring other options.

Bash History:

As mentioned above, you usually have a .bash_history and Bash usually stores the commands you’ve entered in that file. That file is plain text, meaning you can search it, edit it, or even modify it. I’ve written some about Bash’s history before.

Search For Command History By Date
Dealing With Duplicates In Your Bash History
Let’s Delete An Entry In Your Bash History
How To: Not Save A Command To Bash History

This shouldn’t be a foreign concept to my regular readers, though I’ve not written much about Bash recently. This seems like a fine time to cover this subject.

Today, we’re going to change the Bash History Size. There are three variables you’ll need to know. Well, you can get away with just knowing two of them, but we’ll learn all three.

Bash History Variables:

As I said, there are three variables we’ll be using to control the size of your Bash history. They’re all pretty simple and they all control one aspect of the Bash history.

HISTFILE:

The first variable is ‘HISTFILE’ and you can echo this to get the information. The file is normally located at ~/.bash_history but you could change that value and have the history file anywhere you want. You can find the Bash history location with this command:

The output would look similar to this:

HISTFILESIZE:

The next variable we’ll concern ourselves with is ‘HISTFILESIZE’. This is how long, specifically how many lines, will be saved in the .bash_history file. You can your current configuration with this command:

Again, the output from that command might look similar to this:

That means the .bash_history file can be up to 2000 lines. That’s quite a bit, but not far from normal. I think I’ve messed about with it before, so I’m not sure if that’s the default. Just echo the variable to find your current settings.

HISTSIZE:

The next variable is going to be  HISTSIZE which is how many lines your shell should retain in memory. If the HISTSIZE is set to 20, you’ll have 20 lines of history to scroll back to visit. You can get the HISTSIZE settings with this command:

An example output might look like this:

NOTE: This can sort of be overridden by your terminal client. If you have a $HISTSIZE of 1000 and your terminal preferences (some terminal emulator clients have this setting) are set to 500, you’ll only have 500 lines kept in the cache at any one time. However, the number of cached commands is still 1000 – but your terminal will not show them.

We can change these variables!

Change Your Bash History Size:

Now that you know the default size settings for your Bash history, you can go ahead and learn how to change the settings for your Bash history. While it’s possible to change the location of your .bash_history file, there’s no need to.

We’ll worry about our Bash history size, that is how many lines the history file will hold. Anything else is superfluous so we’ll skip it.

The first variable you might want to change is the total number of lines that will be stored in your history file. That’s done like so:

If you want your Bash history to hold 5000 lines, the command would look like the following command:

That won’t take effect immediately. You’d have to reboot or close all the terminals before opening a new terminal for the new settings (reloading what is normally called .bashrc) to take effect. However, you can force the issue with the following command:

If you want to change the value for the maximum number of cached commands a shell can retain in any one setting, you can use the following command:

If you wanted to match the HISTSIZE with the new HISTFILESIZE, that’s easy enough to do. Just enter the following command to change the value for HISTSIZE:

Just like before, you’ll have to tell Bash to reload your settings because they’ve changed. Rather than close things, log out, or reboot, just enter the following command for the HISTSIZE change to take effect immediately:

Note that none of these commands require sudo. If you are having to use elevated permissions, you’re doing it wrong. Well,  you could be modifying the settings for another user but that’d take direct editing of the .bashrc file and you’re better off modifying that file when logged in as the user.

Closure:

Well, that was a pain in the butt to write and format. I hope that it’s not too confusing. I kept typing the variables wrong and this article about changing your Bash history settings required quite a bit of advanced formatting. That takes time, which means this article took time. Still, I wanted to cover the Bash history configuration choices and I wanted to do them all in a single article.

This article is much longer than my normal articles. I dare say that I’ve changed my writing style – again. I’ve been doing longer format articles. Previously, I’d have only covered the $HISTFILESIZE variable and left it at that, without adding many links or much additional information. I do welcome feedback and suggestions.

If you don’t like the longer format, say so. If you do like the longer format, say so. Hell, if you read the closure section say so! I don’t know if I’ll change much based on the feedback, but it’s good to know. Lately, I’ve been pulling down quite a bit more traffic, so something must be working. I’m no SEO expert, so I do what I can and follow directions from people who have had more success. But, the site is coming along nicely.

Amusingly, I’ve had almost no emails on the advertising subject since I added the Sponsorship link. I should mention that it’s possible to sponsor an article even if you’re not a business. I’d just put in a ‘thank you’ notice and link to pretty much anything you want if you did indeed want a link. This site has a solid reputation, but it’s still growing – and growing pretty quickly as of late.

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

Subscribe to Newsletter!
Get notified when new articles are published!
We promise to never share your email!
icon

Author: KGIII

Retired mathematician, residing in the mountains of Maine. I may be old and wise, but I am not infallible. Please point out any errors. And, as always, thanks again for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe To Our Newsletter
Get notified when new articles are published! It's free and I won't send you any spam.
Linux Tips
Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.