8 Bash Tricks you should know

You might have heard a lot of people say that linux shell is very powerful. But have you ever tried to think why do they say that. What secrets do they know about it that you don't? I bet most of you never think this way but if you want to know these so called secrets then follow this golden rule "The more you know about the shell, more you realize its true power". So, lets start with this article where I will tell you about some really cool features of bash that will make working on shell amazingly faster and more interesting for you.

Here is a list of some of the bash most powerful history interaction features. I am sure this will completely change your shell experience.

1. Refer to the previous command

This one could be a problem solver for most of the ubuntu users. Most of the time we type a command which needs root authentication and we forget to prepend sudo in the command. All you have to do in order to avoid typing it again is use "!!" in its place.

$ apt-get install package
$ sudo !!

"!!" actually refers to the previous command.

2. Refer to a command n lines back

It is similar to the previous one. We use "!-n" to refer to the command n lines back. Take a look at this and you will understand. Consider the following sequence of commands.

$ ls
$ cd
$ clear

if you want to run the command cd then you should use "!-2"

$ !-2
cd
$

3. Refer to the most recent string starting with some prefix

I use this one most frequently. I usually forget the full names of the commands and this is how bash helps me to directly run such commands using a slight info about the prefix. Consider the followiing case

$ some-complex-command-name
$ ls
$ cd
Now, I want to use that command again but all I remember is  that it starts with "some", I will then run this command.
$ !some
some-complex-command-name                    ### bash rocks :D

4. Refer to the most recent command with some string in its name

Now, the scenario is slightly different, instead of the prefix, you remember some string that came in the command name. Consider this scenario

$ some-complex-command-name
$ ls
$ cd
Now, I want to use that command again but all I remember is that "mmand" comes in its name, I will then run this command.
$ !?some?
some-complex-command-name                    ### :)

You may remove the trailing ?, if the string is the last part of the command i.e. it is immediately followed by a newline character.

5. Replace a string in the previous command

Typo in commands is a common problem. But you definitely don't want to type the whole command again, can' t even use the tip 1 (!!) either. The only way out is to somehow replace the mis-typed string in the previous command. This is how you do it in the last command.

$ sudo apt-get install wrongpackagename
$ ^wrong^right^
sudo apt-get install rightpackagename

6. Replacing a string in any command

In order to perform this, first of all we will have to mention the command in which the change is to be done. This is done by the !! (last command), !-n (last nth command) and then we will substitute the string. Consider the following scenario

$ sudo apt-get install wrongpackagename
I have mistyped the package name. I will replace "wrong" with "right" using the following command
$ !!:s/wrong/right
sudo apt-get install rightpackagename

as you can see trick 5 was an alternative for this.what if all I remember is that it has install in it

$ !?install?:/wrong/right

7. Select the nth argument of a command

Consider the following scenario:

$ ls /var/www/website/images/large/
this will list out the files but now you want to switch to that directory. You wouldn't want to type that path again, this is how you do this in bash
$ cd !!:1

This means that take the 1st argument of the last command. So, basically all you have to do is to specify the command and then select the argument. Another example, if you want to take the 3rd argument of a command that begins with "abc" (as far as you remember) use this command.

$ /?abc?:3

8. Select a range of arguments from any command

Consider the following case

$ tar -cvzf filename.tar.gz file1 file2 file3 file4 file5
suppose after running this command I realized that I didn't want file4 in filename.tar.gz. This is how I will create a new tar in bash.
$ !!:-2 !!:3-5 !!:$
tar -cvzf filename.tar.gz file1 file2 file3 file5       ### brilliant ;)

Close your mouth now, its not magic this how I did it.

"!!:3-5" means from 3rd to 5th argument of the previous command.

"!!:-2" is an abbreviation for "!!:0-2" i.e. from 0th to 2nd argument.

"!!:$" means the last argument.

In case you want to select everything after the nth argument you will use ":n*" or ":n-$". If you don't want the last argument use ":n-".

I hope this article will be useful for a lot of you. If you have any similar bash trick to share feel free to comment.

10 Comments

Alex (not verified)
December 5th, 2009 05:34 pm
Great commands. I love the "sudo !!" especially :)
ATOzTOA (not verified)
December 7th, 2009 10:11 am

Excellent work... I knew about !!, but never knew it had this much power... For getting last argument from previous command, use $_ $ mkdir some_long_directory_with_lot_of_12345_junk_33_oops $ cd $_

Roy (not verified)
February 15th, 2010 02:50 am
You didn't mention the common Up-Arrow (redisplay the last command). As an alternative to 1) press up arrow then home type sudo press enter. You can use the arrow keys to position the cursor to allow you to edit anything in the command visually. Easier than trying to remember what you typed previously. This little item from my .bash_aliases file goes a little farther than your 2) in that it will display a listing of *all* the previous commands still in your history list that contain the desired string *anywhere* in the command line along with a position in history number. Now you don't have to remember how many commands back the desired command is. I personally have a real good memory; but it's short. # find a command in history - greph # once the command is found execute it immediately by # ! or # display only with !:p then use the arrow keys to edit alias greph='history | grep' if you know the exact command you want is something you entered quite recently you can use # display the history list alias hl='history' but this will create a long list that will mostly scroll off the screen and it will be a pain trying to find anything not fairly recent. Either of these commands can be performed by entering the part after the '=' but creating an alias means less typing.
Anon Linuxer (not verified)
October 4th, 2010 11:31 pm
Oh, I wonder if he realized that he didn't mention it. Good pointing that out. I think the up arrow to scroll through the historical buffer is a bit of beginner/common-sense knowledge, where as what's here is advanced. Good try one-upping the author though. The rest of your post, though good information, is incoherent babel.
Brijin (not verified)
April 26th, 2010 05:52 am
Can i execute a shell script when i enter a particular pattern in my bash shell?? For example like IP addresses?
Anon Linuxer (not verified)
July 19th, 2010 08:00 pm

Those are wonderful tricks! I wish I knew them before.

July 30th, 2010 06:21 pm
To see last command without exec them
!:p

!find:p

!-3:p
David (not verified)
August 21st, 2010 01:29 am
Thank god I use windows. I can't understand any of that stuff and I wouldn't want to have to learn it either.
Roy (not verified)
August 21st, 2010 07:12 am
And Billy thanks you too. He needs people with deep pockets who are too lazy to learn in order to keep up his lifestyle.
Anon Linuxer (not verified)
October 4th, 2010 11:25 pm
You don't *need* this stuff to use Linux; it just allows one to do even *more*. Whereas with Windows you hit an insurmountable ceiling of limitation. You are not a gear head, you're a Windows fanatic.

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <img> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <h1> <h2> <h3> <h4> <h5> <h6> <p> <br>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Image links with 'rel="lightbox"' in the <a> tag will appear in a Lightbox when clicked on.

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.