How To See Bash History

How To Articles

Hey there! Today, I’m going to show you how to access and view your bash history. As a software developer, I rely on the bash history command quite frequently to recall the commands I’ve used in the past. It’s a time-saver and a handy tool to have in your arsenal.

So, without further ado, let’s dive into the world of bash history!

Accessing Bash History

First things first, to access your bash history, you need to open your terminal. Bash, which stands for “Bourne Again SHell,” is the default command-line interpreter on most Unix-like systems.

Once you have your terminal up and running, you can access your bash history by using the history command. Simply type history and hit enter, and voila! You will see a list of your previously executed commands.

But here’s the thing: the history command alone doesn’t display the entire history. By default, it only shows a subset of the most recent commands. To view the complete bash history, you need to tweak a few settings.

Viewing Complete Bash History

Method 1: Editing the Bash Configuration File

The bash configuration file, usually located at ~/.bashrc or ~/.bash_profile, contains various settings for your bash environment. To view the complete bash history, you can edit this file and modify the settings.

  1. Open your preferred text editor and open the bash configuration file. For example, you can use the command vi ~/.bashrc to open it in the vi editor.
  2. Locate the line that starts with HISTSIZE. By default, it’s usually set to a low number like 500.
  3. Change the value of HISTSIZE to the desired number of commands you want to store in your bash history. For example, if you want to store the last 1000 commands, set it as HISTSIZE=1000.
  4. Save the file and exit the text editor.
  5. Restart your terminal or run the command source ~/.bashrc to reload the bash configuration file.
  6. Now, when you run the history command, you will see your complete bash history.

Method 2: Modifying the Bash Environment Variables

If you prefer a more temporary solution, you can modify the bash environment variables directly in your current session. This method doesn’t require editing any files and provides flexibility without making permanent changes.

  1. Open your terminal and run the following command to set the desired value for HISTSIZE:
  2. export HISTSIZE=1000

    This command sets the HISTSIZE to 1000, but you can replace it with your preferred number.

  3. Run the history command, and you will now see your complete bash history.

Conclusion

And there you have it! Accessing and viewing your bash history is a simple yet powerful way to recall and reuse your previously executed commands. Whether it’s to quickly repeat a complex command or to troubleshoot a recent issue, the bash history command comes in handy.

I hope you found this article helpful and that it improves your command-line productivity. Happy coding!