Bash alias example | Linux

by Ramakanta · Published November 26, 2012 · Updated November 21, 2016

We will create a command of our own using the alias command. But before we start, we need to reveal a small command-line trick. It’s possible to put more than one command on a line by separating each command with a semicolon character. It works like this:

As we can see, we have combined three commands on one line. First we change directory to /usr, then we list the directory, and finally we return to the original directory (by using cd -) so we end up where we started. Now let’s turn this sequence into a new command using alias. The first thing we have to do is dream up a name for our new command. Let’s try test. Before we do that, it would be a good idea to find out if the name test is already being used. To find out, we can use the type command again:

After the command alias we give the alias a name followed immediately (no whitespace allowed) by an equal sign, which is followed immediately by a quoted string containing the meaning to be assigned to the name. After we define our alias, it can be used anywhere the shell would expect a command.

While we purposely avoided naming our alias with an existing command name, it is sometimes desirable to do so. This is often done to apply a commonly desired option to each invocation of a common command. For instance, we saw earlier how the ls command is often aliased to add color support: