Tips For Using Bash History

Here are a few ways to use the bash history efficiently. It can make your (linux) life easier, but you should be careful when using some of them:

1. history: this command will show you the last commands you’ve typed, notice that each command (line) has a number, we’ll make use of it in the next tip.

2. exclamation mark + number: typing an exclamation mark followed by a number (e.g. !102) will call (i.e. re-enter) the command at line 102 (this is the number of the command as appears when you invoke the “history” command.

3. bang bang: A pair of exclamation marks (i.e. !!) will be substituted by the last command you ran. This can be helpful for example if you try to type a long command as a user, and it refuses to run because it needs super-user privilleges, then you can simply type:

sudo !!

This will run the previous command with super-user privilleges.

4. exclamation mark + string: typing an exclamation mark followed by a string (e.g. !ec) will run the last command in your history that begins with this string (be careful when using this). For example, suppose you ran this sequence of commands:

echo hello

grep blabla somefile.txt

Now if you run this:

!ec

This will run “echo hello”

5. exclamation mark + question mark + string: I’m writing about this option for your information only, I don’t recommend using it since it can surprise you. If you decide do use it, be very careful:

typing this:

!?ep

Will run the last command in your history which includes the string “ep” anywhere in the command.

6. Dollar sign: suppose the last command you typed was something like this:

command arg1 arg2 arg3

The variable $_ will restore the last argument (i.e. arg3), so you can use it now:

anothercommand $_

and this will be equivallent to typing

anothercommand arg3

Another option is using !$ (exclamation mark followed by a dollar sign), instead of $_ . This is almost the same, except that this will also print the command after “translation” to the shell before executing it.

7. Go through the history of last arguments: This is a shortcut: typing

1
[alt] + .

(this is: alt and a dot), will insert the last argument of the last command. Typing it twice will insert the last argument of the command before the last instead, etc.. For example, suppose you typed this sequence of commands:

ls file1

cat file2

Now, if you type this :

1
rm [alt]+.

(again:

1
[alt] + .

means the alt key and the dot key)

this will actually remove file2.

And if you type this instead:

1
rm [alt]+. [alt]+.

This will remove file1.

Note: You can use

1
[esc]+.

instead, but I find the the alt key much more comfortable to use in this case.


Credits: Command Line Warriors

Credits: shell-fu

If you enjoyed this post, make sure you subscribe to my RSS feed!

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Related posts:

  1. Re-Use A Bash Command With Different Parameters

Tags: ,

Wednesday, January 28th, 2009 Bash

Leave a Reply

my email
my photo
Hi,
My name is Amir Watad. I have a BSc. in biomedical engineering from The Biomedical Engineering school , Technion , Israel, and a BSc. in electrical engineering from The Electrical Engineering school , Technion , Israel.
I'm a Verification Engineer in Mellanox Technologies Ltd.
I love Linux, the Command Line and the OpenSource Community.
I used to write Poems (Arabic) when I was able to find time for this.
January 2009
S M T W T F S
« Dec   Feb »
 123
45678910
11121314151617
18192021222324
25262728293031
SEO Powered by Platinum SEO from Techblissonline

Twitter links powered by Tweet This v1.7.3, a WordPress plugin for Twitter.