Some commands can be quite long, and typing them repeatedly can be tedious and time-consuming. If you find yourself often running lengthy commands, then using bash aliases will come as a huge relief. Essentially, an alias is a shortcut to a Linux command that is defined in the ~/.bashrc file. Once the shortcut is defined, it can be invoked on the bash terminal and display the same output just as the long command would.
Bash aliases save you the agony of remembering long and sometimes complex commands. In doing so, they save you valuable time and help you scale your productivity. In this tutorial, we explore how you can create and use bash aliases. We have used Ubuntu 20.04 for demonstration.
How to create a bash alias
Creating a bash alias is quite straightforward, and the syntax for doing so is as follows.
Defining an alias begins with the alias keyword, followed thereafter by the alias name. After the equals sign, we have the command which is to be aliased enclosed in double quotation marks.
Each command alias is defined on a new or separate line:
Let’s create an alias for a command that checks the available disk space for all the mounted ISCSI disks on your laptop PC. The following df command provides the solution. It provides disk utilization statistics on the mounted physical drives.
$ df -Th | grep /dev/sd
Now, this is quite a long command and can be simplified by creating an alias for it. This alias will then be called on the terminal and still provide the same output.
First, open the ~./bashrc file that is located on the home directory as follows:
$ vim ~/.bashrc
On the last line of the file, define the alias for the command as follows. Here, df is the alias name:
alias df=”df -Th | grep /dev/sd”
Save and exit. To enable the alias and make it available even after a reboot, run the command provided.
$ source ~/.bashrc
You can now call your alias on the terminal as shown
As you can observe, we have managed to generate the same results using a much simpler and easy-to-remember command.
How to disable a bash alias
If you no longer require the alias, you can disable it by invoking the following command on the terminal.
$ unalias name_of_alias
For example, to remove our alias, we are going to run the command:
$ unalias df
This time around, we don’t get our desired output when we call the alias because it has been disabled.
Running long and complex commands is usually tedious and time-consuming. Aliases provide much-needed relief by providing shortcuts to those complex commands. These shortcuts can easily be called on the terminal and yield the same result as the complex command. We trust you now have a firm grasp of creating and using bash aliases.
Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.