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.

Let’s Play Around With The Linux Shell.

Today’s article is a bit of a mystery, as I’m writing it and not sure how it’s going to end up – but it will be about the Linux shell. I’ll try to make it approachable, even for beginners. Let’s see where the article takes us!

I think I’d caution most beginners to stick with what came with their distro (most likely Bash), rather than straying into the world of different shells. Wait until you have a few more miles in your Linux shoes before running to try new shells. It’ll make support easier if you don’t have to compound things by using an alternative shell. Once you’re up to speed, there are some nifty choices.

This seemed like the perfect question to pose ChatGPT. I’ve found a way to make AI useful – or so it seems to me. I asked the AI to explain the Linux shell in two paragraphs. This is what they had to say:

A Linux shell is a command-line interface (CLI) program that allows users to interact with the Linux operating system by entering commands. It acts as an intermediary between the user and the kernel, which is the core of the operating system. The shell provides a text-based environment where users can execute various commands to perform tasks such as navigating the file system, running programs, managing processes, and configuring system settings.

The shell interprets the commands entered by the user and translates them into instructions that the kernel can understand and execute. It also provides features like command completion, history, and scripting capabilities, allowing users to automate tasks and create powerful scripts. There are different types of shells available in Linux, such as Bash (Bourne Again SHell), which is the default and most widely used shell in Linux distributions. Other popular shells include Zsh (Z Shell), Ksh (Korn Shell), and Csh (C Shell), each offering its own set of features and functionalities. Overall, the Linux shell is a fundamental component of the operating system, providing users with a flexible and efficient way to interact with the system.

That looks like a pretty good description to me. If you have any questions, you can certainly leave a comment. You know where to find me so that you can ask questions most anywhere.

Hmm… As I said, I’m not sure where this article is headed. We’ll have to see…

Some Stuff About The Linux Shell:

Pretty much every single one of the new folks reading this will have something in common with those who have used Linux for years. You’re almost certainly using Bash as your shell. Bash stands for Bourne Again Shell. Bash is the Linux equivalent of Bourne Shell that came with Unix back in ye olden days.

If you want, you can open a terminal at this point. Odds are that you have another shell or two available. You can list your shells with the following command:

If you don’t already know what shell you’re using, you can find out quickly enough. Just run this command to find out which shell you’re using:

Let’s pretend you’re using Ubuntu and want to install a new shell. Let’s pretend you are interested in ZSH. That is ‘Z Shell’ and is an alternative that you might consider. The first step would be to install ZSH and you’d do so like this:

Follow the prompts and when you next run cat /etc/shells again, you’ll see that ZSH is an option. Pay attention to the path. It will look like /usr/bin/zsh and you can change to that shell quickly enough. Just run the following command:

Or, for a better example:

You’ll then want to reboot your system. That’s easily done with:

That should do the trick for pretty much everyone, though you’ll need to use your package manager if you’re not using apt with Debian, Ubuntu, Mate, or similar. It’s easy enough to switch to a new Linux shell – but you should probably have a good reason for doing so.

By the way, feel free to leave a comment if you use a different shell, preferably explaining why you’ve chosen to do so. I’ve played with different shells and that was entirely my reason for doing so, I just wanted to play around with them. I stick with Bash pretty much exclusively, or whatever’s installed by default.

Again, I’d caution you newer users to skip this article and just use this pile of words to learn about shells in the first place. If you’ve been using Linux for a while and want to learn something new (or have a specific reason to do so) then you can change your shell as much as you’d like. It’s your computer, you can do anything you darned well want with it!

Closure:

So, I wasn’t sure what this article was going to look like. I just figured I’d do an article about the Linux shell. We all know at least one beginner is going to start mucking about with alternative shells, at least this way we can tell them that we suggested they wait to do so.

Then again, how often did we wait to do so? How often did we go mucking around with things best left untouched until we learned better? At the end of the day, Linux is an OS that suits those who like to tinker, but she can be a harsh mistress when you’re still learning. And, again, how many of us learned by breaking things? I reckon many of us did and that’s how we got here.

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.

Let’s Delete An Entry In Your Bash History

In today’s article, we’re going to learn how to delete an entry in your ‘bash’ history. It’s a useful skill to have, for any number of reasons. It’s not all that difficult and this shouldn’t be a very long article. Read on, my dear readers! Even a long-term Linux user might learn something – but I make no promises!

I’ve previously covered how to remove duplicates from your bash history. It may be worth checking that article out, as most of you are going to be using bash. Sure, there are other shells, but bash is the most common on desktops and servers. So, we might as well learn with bash.

If you don’t know, bash is both a language and an application. Unless otherwise specified, you’re almost certainly using bash when you open a terminal or TTY. 

When you enter a command in the terminal or TTY, it’s saved to your bash history. In fact, it’s saved to the hidden file ~/.bash_history. If you so wanted, you could just open the .bash_history file with a text editor and remove lines as you wished. That works just fine.

However, in this article we’ll be using the ‘history’ command. It’s a handy command, useful for recalling previously entered commands and managing those stored commands. I guess this is really more an article about performing some very basic tasks with the history command. Like I said, it’s good for you to know this sort of stuff – especially as a Linux beginner.

Delete An Entry In Your Bash History:

This article requires an open terminal, like many other articles on this site. If you don’t know how to open the terminal, you can do so with your keyboard – just press CTRL + ALT + T and your default terminal should open right up and be ready for use!

So, with your handy-dandy freshly-cracked-open terminal, let’s just display your bash history. You’re going to do that with just the following command:

You’ll notice that every stored command from your bash history is assigned a number that’s shown on the left (or right, if you use a RTL language). Well, that’s how you delete it. You use that number in the following command:

You can use that command to delete an entry in your bash history, and you can do so as often as you’d like. While you’re there, you can also just plain clear your entire bash history with this command:

That will clear all of your bash history – more or less. Your current session may have not been written to the history at the time you issued that command. It might not be saved until you close the terminal instance. This makes it slightly more complicated to make sure the history is truly empty. However, it’s definitely close enough.

Anyhow, you might want to delete your bash history to remove commands that you’ve memorized. You might want to remove commands that didn’t work. If there are sensitive commands in your bash history, this is a way to remove them, surgically or en masse. There are all sorts of reasons why you might want to go through your bash history to delete past commands – and now you can!

Closure:

Yay! It’s another back-to-basics kind of article. In this one, we learn how to delete an entry in your bash history file. In some cases, it may be easier for you to just do so with a GUI and a GUI text editor. You don’t even need ‘sudo’ to make the changes and removing a lot of entries might be faster with a graphical application. Just remove the lines you don’t want showing up in the history any longer.

Also, I should mention somewhere that you can see the history by just pressing the up arrow, but I suspect folks will already know that. ‘Snot the best way to go about managing them, but you can see and select them, running them again as you wish.

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 own site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

Find Out Which Shell You’re Using

Today’s article is going to teach you how to find out which shell you’re using. Knowing which shell you’re using is important if you’re using an unfamiliar system. Most of the time, it’s something you’d already know, but once in a while you might need to find out which shell you’re using.

As you’re visiting this site, it’s fairly safe to assume you’re using Linux (or at least interested in Linux). In that case, you’re quite likely using Bash. Bash is the most common, from my observations.

If you’re curious, Bash is a replacement for Bourne Shell – and is ‘Bourne Again SHell’. It has existed, thanks to the Brian Fox and the GNU Project, since the late eighties. It’s pretty ubiquitous, pretty stable, and pretty feature complete.

While Bash is the most common, it’s certainly not alone in the field of shell options. There are other shells, from ZSH to Fish, or from Dash to Nushell. In some distros, you might find one of them installed by default, but it’s usually going to be Bash.

So, odds are pretty good that you will already know if you’re using something other than Bash. After all, you’d likely have been the person who installed the alternative. Still, there are a couple of commands you can use when you want to figure out which shell you’re using, for when you do need to know.

Find Out Which Shell You’re Using:

This article requires an open terminal, like many other articles on this site. If you don’t know how to open the terminal, you can do so with your keyboard – just press CTRL + ALT + T and your default terminal should open.

On the off-chance that you don’t know what a shell is, Wikipedia describes the shell as:

A Unix shell is a command-line interpreter or shell that provides a command line user interface for Unix-like operating systems. The shell is both an interactive command language and a scripting language, and is used by the operating system to control the execution of the system using shell scripts.

And now, with your terminal emulator open, let’s go about finding out which shell you’re using. 

The first command you can try would be:

You can also use the echo command in a different way. It’s even a bit more clear with this command:

You can also use ‘ps’ which takes snapshots of current processes:

There are certainly other ways to find out which shell you’re using, but those are a few ways that are easy enough. If you have other ways, please feel free to leave them as a comment.

Oh, and here’s a bonus… If you want to see which shells are installed on your system, you can use the following command:

The output of this would look similar to this:

list of installed shells
This is the defaults from Lubuntu 20.04 LTS. Your output may vary, of course.

As I said, that’ll let you know which shells you have installed. In a future article, we might discuss how to change your default shell – but that’s something I seldom bother with these days. Bash works and works well.

Closure:

Yup… There it is! Another article. Now that the year is up, I’m not sure what to look forward to as a goal. I highly doubt I’ll make it a full two years, but it could happen. We shall see…

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 own site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

Automatically Logout Of Your Shell

For security reasons, you’ll possibly automatically logout of your user sessions. If you didn’t know, you can actually do that with your shell, in the terminal. There’s already a variable (TMOUT) just for this reason, should you want to add it as a layer of security.

Basically, for today, we’re going to set it up so that it automatically logs inactive users out of their shell session. It doesn’t log you out of your complete user session, it just logs you out of your shell – after a set period of activity. It even closes the open terminal windows when it does so.

So, depending on the interval you use, you can set it up to log you out of your shell instances after just a few minutes of inactivity. If you have nosy neighbors, like people physically near your computer, it can be a nice way to make sure things are all locked before you head off to the bathroom.

It’s useful for that sort of stuff. It’s just an added layer of security. I think that it is a pretty handy feature. I’ll explain how to enable it on a user-by-user basis and how to make it system-wide, giving you a choice. It’s actually pretty easy, so read on!

Automatically Logout Of Your Shell:

Like most good things in the Linux world, you’ll need an open terminal to take advantage of this article. If you don’t know how to open the terminal, you can do so with your keyboard – just press CTRL + ALT + T and your default terminal should open.

Both of these ways are pretty simple, in each case you add some text (using nano) to a profile file. The text in either case is the same. If you want to do it for just one user, the user you’re currently using, then run the following:

Add the following:

So, if you wanted it to be 10 minutes of inactivity before being logged out, you’d use TMOUT=600, because 600 seconds is 10 minutes. As you’re using nano, you can press CTRL + X, then Y, and then ENTER to save the file.

You’ll then force the profile to load, the command taking effect immediately, with this:

If you want to do it with the full system, the online guides will tell you to edit /etc/profile and that it’ll work if you do. My experiences are different and this is tested across multiple systems. You’ll be editing /etc/bash.bashrc, just like you did above but with sudo. (Using /etc/profile has not worked for me.)

Again, you add ‘TMOUT=600″ or however many seconds you want to wait. Personally? I scrolled to the bottom of the file, made a new line, and added the text that way. You could be all professional and add a comment indicating when and why you were there. I did nothing of the sort.

Unlike the first command, you’ll not be able to reload the second method (system-wide configuration) with ‘source ~/…’. As near as I can tell, you’ll have to restart the system for the changes to take place. If someone has a way to load it without rebooting, I’ll update the article. Please leave a comment if you do know of a way!

Closure:

There you have it, another article! This one tells you how to automatically logout from your shell. I’m not sure if it works for all shells, so feel free to test and see what sort of results you get. I’m pretty sure the 2nd option could be reloaded without rebooting, but I can’t think of which command. Which service would need restarting? I dunno?

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 own site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

Linux Tips
Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.