8 Bash Tricks

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 😀

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-“.

g33kadmin

I am a g33k, Linux blogger, developer, student and Tech Writer for Liquidweb.com/kb. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.