• About
  • Advertise
  • Careers
  • Contact
Thursday, February 19, 2026
  • Login
No Result
View All Result
NEWSLETTER
Blacklight News
  • Home
    • Home – Layout 1
    • Home – Layout 2
    • Home – Layout 3
    • Home – Layout 4
    • Home – Layout 5
  • World
  • Home
    • Home – Layout 1
    • Home – Layout 2
    • Home – Layout 3
    • Home – Layout 4
    • Home – Layout 5
  • World
No Result
View All Result
Blacklight News
No Result
View All Result
ADVERTISEMENT
Home National

Bash to the Future: A Guide to Setting Up and Using Bash History

by Ezekiel
November 20, 2022
in National
0
0
SHARES
13
VIEWS
Share on FacebookShare on Twitter

Just like Doc Brown needed a flux capacitor to travel back in time, you need a bash history file to keep track of your previous commands. Without it, you’ll be stuck trying to remember what you did last month or even last week. If you’ve ever tried to search for a command you typed into the shell days earlier by hitting the up arrow key, you know it’s not worth the headache. Typing the up arrow key repeatedly until finding what you’re looking for can be a huge pain.

Utilizing the history command is your solution to this problem. The ‘history’ command is a useful tool that logs all the commands you type in the terminal. This can be helpful if you want to retrace your steps or avoid having to retype past actions.

The history command is more than just a convenient way to run old commands; it can also be used to speed up your workflow by allowing you to quickly edit and reuse previous commands. For example, you can use the history command to quickly substitute arguments in previous commands, saving you time and keystrokes. Learning how to use the history command effectively can help you work more efficiently at the command line.

To begin try simply viewing your history file by typing ‘history’ in the terminal. By default, this will print the last 500 commands you’ve typed (this can be changed – see ‘Setting History Defaults’, below). If you want to view your entire history file, use the ‘-a’ option:

history -a

This will print every command in your history file, regardless of when it was executed.

You can also use the ‘-c’ option to clear your history file. This is useful if you want to start fresh or remove sensitive information from your command history. Be warned that using this option will delete all entries in your history file, so use it with caution.

history -c

Now that we covered the very basics let’s take a look at setting up some history defaults.

Setting History Defaults

Your history is stored in a file called ‘.bash_history’ in your home directory. By default, this file is only written when you exit your shell session. This means that if you terminate your shell with Ctrl-D or exit, the commands you executed during that session will be saved to your history file. However, if you terminate your shell abnormally (for example, with Ctrl-C), the commands from that session will not be saved.

You can change this behavior by setting the HISTFILE environment variable in your ~/.bashrc file.

Add The following line to the end of your ~/.bashrc file:

export HISTFILE=~/.bash_history

Now your history will be written to the ~/.bash_history file every time a command is executed, regardless of how you terminate your shell. Don’t forget to source your ~/.bashrc file for the changes to take effect:

source ~/.bashrc

Another useful setting is to increase the size of your history file. By default, the history file can store up to 500 commands. If you find yourself frequently running out of space, you can increase the size of the file by setting the HISTSIZE environment variable. Add the following line to your ~/.bashrc file:

export HISTSIZE=5000

This will increase the size of your history file to 5000 commands. You might say to yourself wow 5000 lines is excessive, and you might be right, but it’s easy to change the number to whatever you like. You can easily change the value of HISTSIZE to whatever you want if you want to decrease or increase the size of your history file.

In addition to changing the size of the history file, you can also change how often it is written. By default, the history file is only written when you exit your shell. This means that if you lose your connection or your shell session is terminated abnormally, the commands from that session will be lost.

If you want to change this behavior so that the history file is written after every command, you can set the $PROMPT_COMMAND environment variable. Add the following line to your ~/.bashrc file:

export PROMPT_COMMAND="history -a"

Now your history will be appended to the ~/.bash_history file after every command, regardless of how you terminate your shell.

One last thing to note is that you can use the ‘-w’ option with the history command to manually write the contents of your history file to disk:

history -w

This can be useful if you want to save your history without exiting your shell.

Ignoring duplicates

If you frequently type the same commands over and over, you might want to consider setting the HISTCONTROL environment variable. This variable can be used to ignore duplicate entries or commands that start with a space. Add the following line to your ~/.bashrc file:

export HISTCONTROL=ignoreboth:erasedups

This will tell bash to ignore both duplicate commands and commands that start with a space.

If you want to take this a step further, you can also tell bash to ignore certain commands by adding them to the ~/.bash_history file. Any line in this file that starts with a space will be ignored. This can be useful for ignoring sensitive information or frequently used commands that don’t need to be stored in your history.

Retrieving your bash history

Once your history is configured the way you want, it’s time to start using it. The first thing you need to know is how to retrieve your history. This can be done with the ‘history’ command:

history

This will print a list of all the commands in your history file. If you want to see the last n commands, you can use the -n option:

history -n 10

This will print the last 10 commands in your history file.

To search your history for a specific command, you can pipe grep to the history command:

history | grep "command"

This will search your history for the specified command. If you want to execute a command from your history, you can use the ‘!’ operator:

!10

This will execute the 10th command in your history file. You can also use the ‘!!’ operator to execute the last command in your history:

!!

If you want to execute a command that contains spaces, you will need to wrap it in quotes:

!"command with spaces"

Searching through Your Bash History

Sometimes it can be tedious to scroll through your history using the history command, especially if you have a large number of commands. For this, you can use the ‘Ctrl-R’ key combination to search your history.

When you press Ctrl-R, the command line will display ‘reverse-i-search’, followed by a prompt. You can then type in a search term to search your history for commands that contain the term. As you type, bash will show you all the matching commands from your history file. To execute one of the commands, just press enter.

If you add the following line to your ~/.bashrc file (or, if you’re familiar with setting up aliases from our previous article, add this alias to your ~/.bash_aliases file), then you can also seat up an alias to grep through your history:

alias h='history | grep $1 '

With this alias, you can search your history by typing ‘h’ and your search term.

h "command"

Using Your Bash History to Improve Productivity

Once you have your history set up, you can start to use it to improve your productivity. Here are a few tips:

1. Take advantage of the grep alias we mentioned earlier to quickly search for a command from your history.

2. Use the Ctrl-R key combination or ‘!!’ operator to quickly execute previous commands without having to type them in again.

3. When typing long commands, take advantage of the auto completion feature by pressing tab after typing part of the command. Bash will attempt to autocomplete the command based on what’s stored in your history file.

4. Whenever possible, take advantage of aliases and functions that you’ve defined in your bash profile (see our article on bash aliases and functions for more information). This will help you quickly execute commonly used commands.

5. When typing in a lot of commands, use the ‘Ctrl-U’ key combination to clear the current line. This can save you time if you realize you’ve made a mistake and need to start over.

Now that you know how to configure and use your bash history file, you can start saving time! In the future, we’ll dive into more advanced topics like history substitution, history expansion, and setting up your prompt to display your history. Until then, go ahead and make like Marty McFly and explore the future of your bash history!

Tags: LinuxNationalTech
Ezekiel

Ezekiel

Next Post
The Dark Origins of Thanksgiving And The Mystic Massacre

The Dark Origins of Thanksgiving And The Mystic Massacre

Leave a Reply Cancel reply

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

Recommended

Unlocking The Power of Vim: Getting Started

Unlocking The Power of Vim: Getting Started

3 years ago
Black Balled At the Bank: 71 Year Old Woman Accuses Bank of Discrimination

Black Balled At the Bank: 71 Year Old Woman Accuses Bank of Discrimination

3 years ago

Popular News

  • Are Automated Journalism Tools Destroying The News Industry?

    Are Automated Journalism Tools Destroying The News Industry?

    0 shares
    Share 0 Tweet 0
  • Biden Announces Proposal to End the Student Loan Debt

    0 shares
    Share 0 Tweet 0
  • North Carolina Teen Earns Title Of Fastest Runner In America 

    0 shares
    Share 0 Tweet 0
  • Florida Man Shoots Student Over $1

    0 shares
    Share 0 Tweet 0
  • The Science Behind Why Janet Jackson’s Rhythm Nation Can Cause Laptops To Crash

    0 shares
    Share 0 Tweet 0

Connect with us

Facebook Twitter Youtube RSS

Category

  • National

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
No Result
View All Result
  • Home
  • World
  • National
  • Gaming
  • Movie

If you're looking for a way to stay up-to-date on the latest news, events, and happenings, then subscribing to our newsletter is the perfect solution! Not only will you receive regular updates, but you'll also get exclusive access to articles, tips, and advice that you won't find anywhere else. So what are you waiting for? Subscribe today!