Create and use alias in Ubuntu

In this article, we would see how to create and use alias in Ubuntu distribution. alias is basically a shell built-in command-line utility. It is used to create shortcut for the commands.

Let’s understand the concept with the help of an example. Consider a scenario, wherein we need to issue the following command in the terminal repeatedly –

cat /etc/passwd | grep $USER

It wouldn’t be feasible to issue the complete instruction all the time. That is where, alias command-line utility comes to rescue. Since, it is used to create shortcut for such instructions/commands. Thereby, bringing efficiency in the work we do.

Next, we will see how its done. There are two methods we would discuss – through terminal and editing ~/.bashrc configuration.

Create and use alias in Ubuntu

Method I. Through terminal –

We continue with the above mentioned example. We would like the ab as the shortcut for the command –

cat /etc/passwd | grep $USER

So, in terminal issue the following –

alias ab='cat /etc/passwd | grep $USER'

Now, from here till we exit from shell – issuing ab in terminal will result in the output which we get from cat /etc/passwd | grep $USER

But, once we exit the shell. All changes are lost.

Method II. This time around, we would edit the configuration file ~/.bashrc

This will ensure, any changes we make stay permanent.

Use a text-editor to edit ~/.bashrc, we have used nano. And, append the file with the following –

alias ab='cat /etc/passwd | grep $USER'

Save and exit.

Lastly, use source built-in shell command to execute commands in current shell.

source ~/.bashrc

ab command is now capable to get us the desired outcome.

So, even after we logout – all the changes made will stay there.

In conclusion, we have discussed how to create and use alias in Ubuntu distribution.

Additional Info –

To check the alias we have set, just issue the following in terminal –

alias

To remove the alias from the current shell –

unalias -a ab

Note: This will remove alias from temporary configuration of Method I. If we have made an entry in ~/.bashrc configuration then, the next time we open a shell. The alias we created would still be there.

Similar Posts