How to Use Bash History in the Linux or macOS Terminal

Bash shell is the default linux distribution you get with a Linux image, including MacOS. It is also available for installation on Windows 10. Just like a smart shell would do, it save the history of commands you run and save them in a history file. 

Bash maintains the list of commands internally in memory while it’s running. They are written into .bash_history on exit.

If you want to make sure that they’re always written immediately, you can put that command into your PROMPT_COMMAND variable:

export PROMPT_COMMAND='history -a'

Setting History Defaults

Before we start using the history, let’s modify some bash settings to make it more useful.

Bash allows us to adjust the number of previous commands that it stores in history. It handles it in two different ways: the HISTFILESIZE parameter configures how many commands are stored in the history file, while the HISTSIZE controls the number stored in memory for the current session.

Open the ~/.bashrc file in an editor to change these settings:

nano ~/.bashrc

Search for both the HISTSIZE and HISTFILESIZE parameters. If they are set, modify the values. If these parameters aren’t present yet, simply add them. For our purposes, we can easily get away with saving 1000 lines to disk and loading the last 500 lines into memory. This is a conservative estimate for most systems, but adjust it down if you see a performance impact:

HISTSIZE=500 HISTFILESIZE=1000

By default, bash writes its history at the end of each session, overwriting the existing file with an updated version. This means that if you are logged in with multiple bash sessions, only the last one to exit will have its history saved.

Using Keyboard shortcuts

To go over the bash commands we ran last, there are many key strokes we can use to our advantage and save time.

  • Down Arrow or Ctrl+N: Go to the next command. Hitting same key multiple times will show the commands we last ran.
  • Up Arrow or Ctrl+P: Go to the previous command. Hitting same key multiple times will move backwards through the commands we’ve used.
  • Alt+R (or Ctrl + R for MacOS): This is also called as reverse search and is useful. This can be helpful when you know only partial commands you typed before but didn’t remember.

As shown, ‘which’ was searched and bash looked for the last matching command in history.

View Bash history

Looking at history of commands is easy. Just run the history command.

history

We can narrow our search using a pipe like grep command and a search term.

history | grep which

We can even look at only last five used commands by using a pipe through the tail command.

history | tail -5

Or, we can even truncate the tail command and simply pass the number of commands we want to look at.

This way, we can simply show last four commands executed in current bash shell.

Executing nth command from History

Printing the bash history is nice but it doesn’t really help us access those commands easily, except as a reference.

If we want to execute nth command from history, like 2nd command we will simply do

This will immediately recall and execute the command associated with the history number 2.

Conclusion

We now have a good idea of how we can (or should) leverage the history operations available to us with bash. Some of these will probably be more useful than others, but it is good to know that bash has these capabilities in case we find ourself in a position where it would be helpful to dig them up.

If nothing else, the history command alone, the reverse search, and the simple history expansions should help us speed up our work flow.

Miguel

I started this tech blog back in 2011 as a place to write down processes I took to fix my client systems and network. Now I write some tips and tricks to help others with the tech issues that one might encounter.

You may also like...