Some commands in Linux are long and repetitive. Aliases enable us to make them shorter by mapping a long command to a shorter one. In this article, we discuss aliases in Linux.
Table of contents.
- Listing aliases.
- Creating aliases.
- Creating permanent aliases.
- Deleting aliases.
- Multiple aliases.
While working on the Linux command line, we sometimes write very long commands which are sometimes hard to remember. By using aliases we can create a short command that is easily memorable and can perform the same task as the long command or groups of commands. We can think of this as a hashmap with key-value pairs whereby the key is an alias we create and the value is a long command or group of commands that are hard to remember. Now, when we call the alias it executes its value(long command).
By default, Linux will have already created aliases. We can list them by executing alias command:
A basic use case is testing for network connectivity by pinging a remote host/server. For this, we have to remember a host IP address every time. To create an alias we write:
$ alias pg="ping -c 4 188.8.131.52"
Now when we execute the pg command, its value will be executed as shown.
Now let's create an alias for system updates and upgrading:
$ alias update="sudo apt-get update && sudo apt-get upgrade"
Now to update and upgrade Linux packages we write:
Creating permanent aliases.
As you have noticed, the aliases we created won't work in a new terminal session. We can create permanent aliases by editing the ~/.bashrc, the bash config file.
Let's add the alias for updating and upgrading the system:
$ sudo vim ~/.bashrc
After the file is open we paste the following line and save:
alias update="sudo apt-get update && sudo apt-get upgrade"
Now even if we restart a new terminal session or reboot the computer, the aliases we created will persist.
Note that, the ~/.bashrc if the configuration file for the bash shell, other shells such as zsh have their configuration files i.e ~/zshrc
If we want to delete an alias we previously created, we use the unalias command which is followed by the name of the alias we created.
For example to delete the ping alias we write:
$ unalias pg
pg is what we named it previously. Now if we list current aliases, we won't see it.
Sometimes we can have a lot of aliases and this makes managing them harder. For convenience it is usually better to create a separate file for aliases then call this file in the ~/.bashrc file.
For example, a system administrator has to make sure that three servers are always reachable at all times. For this, he/she has to memorize three IP addresses and perform three pings frequently.
In such a situation we create an alias file for testing reachability, ~/.reach_net and create the aliases there.
After this we add the following to the ~/.bashrc file:
if [ -e ~/.reach_net ]; then source ~/.reach_net fi
Then we source the ~/.bashrc file for this to work:
$ source ~/.bashrc
Now we to execute the three commands without having to remember the IP addresses or hostnames:
$ svr1 && svr2 && svr3
Other alternatives to this could be shell scripting whereby we place all the long complicated commands in a shell script and execute it. For repetitive tasks, we can create cronjobs that run occasionally. We can also use the history command which gives us all previously executed commands.