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.
Creating a bash alias is quite straightforward, and the syntax for doing so is as follows.
alias alias_name="command_to_run"
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:
alias alias_name1="command_to_run"
alias alias_name2="command_to_run"
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:
$ df
As you can observe, we have managed to generate the same results using a much simpler and easy to remember command.
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.
This article covers the procedure of creating and using bash aliases. 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.
This tutorial shows how to create and add aliases permanently to your bash shell on Linux and Unix-like systems.
To Create Bash Aliases
Creating aliases in bash is very straight forward.
The syntax is as follows:
alias alias_name="command_to_run"
An alias declaration starts with the alias keyword followed by the alias name, an equal sign and the command you want to run when you type the alias.