Mastering the Command Line History in Linux

If you spend a lot of time working on Linux, it makes sense to learn how to optimize your use of the command line options – especially the time you spend typing in commands. Most Linux users are familiar with the basic Linux command line history. The simplest way to avoid typing out a command is to hit the up arrow key until you find your command, or to use the simple !! for the last executed command. Today we’ll show you a few relatively unknown, and yet effective ways you can use Linux history to speed up the way you work.

Say you ran a command that required root privilege, but you forgot to do a sudo. Rather than type it out again, or navigate up and then left, here’s what you can do

If you want to save the previous command into a new script, here’s how you can do that, without having to type it out


Having access to the last run command has limited usage. Sometimes you would want to search through the history to find an obscure or really large command you’d typed out.  A simple ![keyword] search lets you access it. For example, you had a large command that you’d run in the morning, and you don’t quite remember the options you gave, but you still want to run it again.  If this is what a screenshot of your history from that time was:


So how do you run that command again? Just do:

Sometimes you may not want to execute an earlier command, but just carry over the argument. You can use the “!$” operator like shown below.


If there were multiple arguments to your earlier command, and you want to access only the first, you can use “!^

To access argument number x, just use “![prev_command]:[argument_number]“. The example below shows it more clearly.

What if you have some command you don’t want to show up in the history at all – if you just want to remove duplicates and have a cleaner history. You can set the HISTCONTROL environment variable to handle this. Here’s how to do it.

At times, you may want to prevent simple commands form appearing in the history, like “ls” or “pwd“. In such cases, you can prevent them from appearing in the history by using the HISTCONTROL command again, like we did in the above example. However, it’s usually not a good idea to hide a command from appearing in the history.

To hide a command, you need to first set the HISTCONTROL to ignore spaces, i.e. ignore all commands that start with space. Then when you type the command, make sure to add an extra space before you type. This will prevent history from remembering the command.

Just in case for some strange reason, you may want to disable your Linux history entirely. To do that, you can use the following command:

If you search for your history now, it will be blank. Note that the HISTSIZE command can also be used to change the size of your history. If you wanted to store more commands in history, open up the .bash_profile file in your Home directory and change the HISTSIZE (make it bigger or smaller).

Image credit: bash command-line linux shell terminal , Introduction to the Command Line cover