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.
You need to put bash shell aliases in the ~/.bashrc file ($HOME/.bashrc) file executed by bash for non-login shells. On most modern Linux distros, you may want to put all your bash alias definitions into a separate file like ~/.bash_aliases, instead of adding them in the ~/.bashrc file directly.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related Bash Aliases queries on Ubuntu Linux Systems.
In this context, we shall look into how you can create and use bash aliases on Ubuntu Linux System.
How 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.
[Need urgent assistance in fixing Software Packages on any Linux Distribution? We can help you. ]