Skip to main content
Bash

5 Simple Bash History Tricks Every Linux User Should Know

Effectively using bash history will save you plenty of time in the Linux terminal.

β€” Abhishek Prakash

Warp Terminal

Whether you are a bash beginner or expert, you cannot go on working in the command line without using the super useful bash history feature.

You probably are already aware that if you use the up or down arrow keys in Linux terminal, you can go through the commands you had run earlier.

This is available thanks to the bash history command.

1. View your bash history

The simplest way to see the commands you had typed earlier is to use the command history.

history

It will show a hundred or perhaps a thousand commands stored in your history. The size depends on HISTSIZE environment variable.

If you don't want your screen cluttered, you can limit the number of lines it displays. For example, to show only the last 7 commands from the history, use it like this:

history 7
πŸ’‘
The ~/.bash_history file stores the command history in plain text.

2. Run command from bash history

When you are looking at the history, you can run a command by using its number with !, like this:

!#

You can rerun the last command with !!. A good use of this comes when you forget to use with a command and then quickly use it like this to run with sudo:

sudo !!

You may also run the last command starting with a certain text like this:

!text

Suppose you ran echo $HISTCONTROL earlier and you want to run it again. You can use it like this:

!echo

And the above command will run the last ran echo command from your history.

There is a better option to search through the history, and I'll show it you in the next section.

πŸ’‘
If you are not sure of the command, instead of running it, print it by adding :p to the end of it. So you use it like !echo:p, !23:p etc.

3. Searching through bash history

You may think that it is easier to use grep command for searching something through history like this:

history | grep text

A better and super handy way to search through history is to use the ctrl+r keys to start the reverse search and type the string you are looking for.

You can cycle through the choices by pressing ctrl+r repeatedly and when you find the command you were looking for, press ctrl+o or simply enter key to select and run that command.

For example, the example below searches for a command that had 'aud' in it.

If you don't find the command, change your search string or press ctrl+g to come out of the reverse search.

DigitalOcean – The developer cloud
Helping millions of developers easily build, test, manage, and scale applications of any size – faster than ever before.

Get started on DigitalOcean with a $100, 60-day credit for new users.

4. Reuse arguments from previous commands in your history

Here is another cool stuff you can do with bash history. You can use arguments from previous commands in a new command.

You can use the last argument of the previous command like this:

new_command !$

For example, say you were reading a file with the less command and you decided you need to edit this file. Instead of typing it entirely, you use the !$ shortcut.

The above option gives you only the last argument of the previous command. If you want all the arguments, use this:

new_command !*

5. Cleaning your history of redundant entries

There is an environment variable called HISTCONTROL that help you with redundant entries in bash history. You can assign one of the three values to it:

  • ignorespace: With this variable set, you can run a command by putting a space before it. The command runs as usual but won't be included in the history.
  • ignoredups: If there are two are more identical commands ran consecutively, only one of them will be recorded in the history.
  • ignoreboth: Sets both of the above mentioned features.

You should set the variable in your bashrc file so that it is set every time you use bash shell.

Too many commands in your bash history? You can clear with the option -c. This will clear the present history of bash and start adding commands afresh from now onwards.

history -c

Bonus Tip: Handling the 'missing commands' from history

Do you get the feeling that not all the commands you enter are recorded in the bash history? That's because you probably had more than one terminal session opened on the same system.

Here's the thing you should know:

  • If you have more than one terminal or tab open, the bash history of one session won't be available in the other one. If you type cd in one session, pressing the arrow key won't show it in the other ones.
  • When you close the terminal and tabs, only the history from the last closed session is recorded in the ~/.bash_history file. Command history from other sessions is lost.

You can change this by using the append mode withΒ export PROMPT_COMMAND='history -a'Β in your bashrc file. It adds the just run command to the history file before showing the prompt (that $ sign on the terminal).

Conclusion

There is a lot more to bash history than these tips I discussed here. You can always refer to the man page of the history command for additional info. You may also want to learn about history builtins, if it interests you.

I hope you find this article helpful. If you have a favorite bash history feature, do share it in the comments.

Abhishek Prakash