Dealing with bash history - gHacks Tech News

Dealing with bash history

If you are a Linux user who frequents the command line, you will most likely have heard of the bash history. This is a special system that keeps a history of all commands run from bash. This has many pros and only a few cons. One of the cons is that, if you are logged onto your machine, someone can see your command line history. There are plenty of situations where you might not want this to happen. In that case, you have to delete your history.

Let's take a look and see how this is done.

Before we start...

There is always a "before" right? In this case I wanted to touch base with everyone to make sure it is know how you can work with the bash history. There is one way the bash history is immediately useful to the user. The bash history makes it easy for recalling the last commands run. If you open up a terminal window and start hitting the up arrow on your keyboard you can run through all of the commands contained in your bash history.

So all you need to do is keep hitting the up arrow until you see the command you wanted to run. This will save you time by keeping you from having to type long commands over and over. It can also help you troubleshoot by showing you what commands you have recently run, which will allow you to deduce what has happened to your machine.

How to delete the history

There are two steps to deleting your bash history. The first step is to delete the ~/.bash_history file. This won't work completely because there will still be a history stored in RAM. So, to get rid of THAT history you have to run the command history -c. Once that command is issued, your bash history will be completely clear.

Not keeping your history

What if, for whatever reason, you do not want your bash history to be retained at all? You can do this. You can do this for either all users or specific users. To turn off bash history for all users you would need to issue the command:

echo "unset HISTFILE" >> /etc/profile

To turn off bash history for a single user issue the command:

echo "unset HISTFILE" >> /home/USER/.bash_profile

Where USER is the user you want bash history turned off.

Once you've done either of the above, bash history will be turned off for good.

History size

Remember, you can control the size of your bash history by editing the lines in your ~/.bashrc file:

HISTSIZE=1000

Change the entry to match the amount of commands you want to retain in your history.

Final thoughts

Bash is an amazing and powerful tool for any and all Linux users. Although you do not have to ever touch the command line, in modern Linux desktops, you may someday have to use the command for either a server or remote access to another desktop. For whatever the reason, you will want to either take advantage of the bash history.





  • We need your help

    Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.

    We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats (video ads) or subscription fees.

    If you like our content, and would like to help, please consider making a contribution:

    Comments

    1. Carlo said on November 4, 2010 at 6:36 pm
      Reply

      That’s a nice piece of information which could come back useful, especially when using sensitive commands in a shared environment. Thanks for sharing then!
      Regards
      Carlo

    2. JohnTash said on November 16, 2010 at 5:07 am
      Reply

      A couple random tips regarding bash history. Instead of repeatedly pressing the Up key, try pressing CTRL+R, and then start typing a command you want to re-run. It will search through your bash_history and put it on the command prompt. So you could hit CTRL+R, type ‘grep’ and then keep hitting CTRL+R until you get to the command you want to re-run.

      And if you use screen a lot, or just multiple shells in general you might notice your .bash_history file gets overwritten by the most recent shell. Use chattr +a ~/.bash_history to set the append-only flagm, and then everytime a shell closes, it will append to the bash_history file instead of overwriting it completely

    Leave a Reply