Quick Bash Command Tricks

Bash is the common shell environment used on Linux servers. If you manage a server, there is a good chance you will need to drop to the command line at some point and encounter Bash or one of its alternatives. The following are some basic commands that can help speed up your bash experience.

Reload the previous command

Let us suppose you just ran the command:

$ apt-get install package-name

But you are on a system that needs “sudo” before running a program that requires root access. You could type the whole thing again and put “sudo” in front, or you could use “!!” to automatically load the previous command:

$ sudo !!

Load commands from a number of lines back

If you ran a command and then ran three more, for example, you may want to refer back to that first command. A quick way to do that is to use “!-n”.

$ !-4

This would go four lines back.

Load a command with a specific prefix

You can also load commands from your command history that match the prefix you specify. For example, if you ran the command “this-is-my-command”, you can match the “this” portion of it to find it:

$ !this

In part two of this guide, we will look at some other equally useful bash commands.