How To Use Bash History to Improve Your Command-Line Productivity

00 - banner

Whether you’re new to the Linux command-line or you’re a seasoned veteran, these tricks will help turn your text-based meanderings into full-blown marathons. Save time, speed up your productivity, and enhance your Linux-Fu, all at once!

History Commands

The majority of these tricks utilize the ‘history’ command in some way, so let’s cover that one first. Just type:

history

It’s that simple! You’ll output similar to the following:

01 - history

To execute a specific command from your history, you can just type an exclamation point followed by the number of the command as listed by history. Here, I’ll re-execute command number 510.

!510

02 - history ex1

By the way, an exclamation point is referred to colloquially as a “bang.”

You can also refer to a command by how long ago it was run. Next, let’s execute whatever we typed three commands ago.

!-3

03 - history ex2

Quick Substitutions

Let’s say you want to rerun your previous command. Just type two exclamation points. This is perfect for when you run a command that needs super-user privileges and you forgot to do that. Just give it the old “sudo bang bang” treatment:

sudo !!

04 - sudo bang bang

Let’s say you want to run a command with the last argument you used. Typing it out is too tedious. Just use a “bang dollar” to substitute your last argument automatically.

cd !$

05 - bang dollar

What if you ran a command with two arguments and you want to run the first one? “Bang caret” to the rescue! This works well when you make a backup of a config file and then want to edit it.

nano !^

08 - bang caret

Next, let’s search for a specific command from your history. Ctrl+R will search backwards for whatever you type. It will autocomplete as you type, and you can scroll through with the arrow keys to find the specific instance you’re looking for.

06 - ctrl r

If you know the last command you ran with a specific keyword, you can skip the search process and pare down your key presses by at least one.

!keyword

07 - bang keyword

Now, for the power substitution: you can substitute an argument for your last specific command using something similar to the previous method. Just add a colon and the number of the argument.

ls !ln:2

Here, I substituted the second argument from my last ‘ln’ command.

09 - bang keyword colon arg

As you can see, I used a Ctrl+R lookup to help illustrate where that argument came from.

Cleaning Up Your Tracks

If you want to clear your history, use this:

history –c

And if you want to disable history altogether, use this:

HISTSIZE=0

To re-enable it, you can just change that value from 0 to something else (the defaults are usually 500 or 1000).

But why should you choose between all or nothing? Use the following command to make sure that any command that leads with a space does not get recorded in your history.

HISTCONTROL=ignorespace

In the fourth line down, I lead the ‘cd’ command with a space.

10 - histcontrol ignorespace

Also, notice that I listed only the last 4 history entries by using a pipe (|) and the ‘tail’ command. You can change 4 to anything else to get the same effect yourself.

No Duplicates Here

I love using Bash’s history. The one thing I don’t like is duplicates. Use the following command to ignore duplicate entries:

HISTCONTROL=ignoredups

11 - histcontrol ignoredups

If you want to use both ‘ignorespace’ and ‘ignoredups,’ you’ll need to use ‘ignoreboth’.

HISTCONTROL=ignoreboth

Pretend to Be Busy

As a bonus for you How-To Geek readers, I’ll include the following, completely irrelevant command:

cat /dev/urandom | hexdump -C | grep “ca fe”

This will continually generate random characters and values in a specific format, which is nice to stare at for therapeutic reasons.

12 - coffee

It also can come in handy in case you want to look busy or take a quick coffee break at work. Don’t be too responsible with this one, guys.  ;-)

Which of these tricks do you like best? Have a favorite we didn’t list here? Share your thoughts in the comments!

Yatri Trivedi is a monk-like geek. When he's not overdosing on meditation and geek news of all kinds, he's hacking and tweaking something, often while mumbling in 4 or 5 other languages.

  • Published 03/4/11
More Articles You Might Like

Enter Your Email Here to Get Access for Free:

Go check your email!