SEARCH

How-To Geek

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

Comments (10)

  1. Rob

    I also like the search and replace ^old^new command for fixing mistakes without typing the whole line in again. :)

  2. Rich

    Thanks for this. VERY help full.

  3. Ironcode

    I like this site more and more each day. Your great work made it possible for me to make the move to the full on Linux. This is great stuff, keep it coming!

  4. SurfMan

    Using the arrow-keys does not work to scroll through the history (at least not on Fedora 14). Repeatedly hitting CTRL-R will do just that.

  5. Adithya Kiran G

    Use can use, alt+. or esc+. to place the last argument of the previous commands instead of !$. similarly instead of !^ use can use the alt+1+. to place the first argument, alt+2+. to place the second argument and soon so forth.. alt+0+. will place the command name.

    The problem wiht !$ is, till the command is executed you never know what !$ is. and bash starts executing as soon as it expands what !$ is. But if you want to know what !$ before executing, use the following tricks.

    # ls howtogeek
    # shopt -s histverify
    # cd !$
    # cd howtogeek

    The other way is, use the following magic-space trick which i think most of us never know

    # bind Space:magic-space
    # cd !$

    voila, it places what !$ is.

    Done..!

  6. Adithya Kiran G

    Guys, for the second trick, after typing !$ dont forget to hit space bar

  7. Tony Glez

    I tested cat /dev/urandom | hexdump -C | grep “ca fe” with a lotery nomber something like cat /dev/urandom | hexdump -C | grep “12 14 28 32”, While It was running, It was looking the htop screen, I said wow it does not consume resources but after 10 min, htop said that my processors and memory were ok, but my computer became very hot

    $sensors

    acpitz-virtual-0
    Adapter: Virtual device
    temp1: +98.5°C (crit = +100.0°C)

    I killed the process and the temperature got normal around 45 °C.

    Is it about specific hardware? I have a Dell Vostro 3500 – Ubuntu 10.10

    Thank you for the post, I enjoyed it.

  8. asdf-chan

    Allways delete bash history! First clue to understanding a cracked system is reading the bash history

  9. thorstenmz

    Thanks a lot for the post and the responses!

    I can verify what SurfMan said on my Ubuntu 10.10 system.

  10. Qlice

    So many tricks

Get Free Articles in Your Inbox!

Join 134,000 newsletter readers

Email:

Go check your email!