With Ubuntu, you can customize your long Bash commands by using short and pleasant aliases as replacements. When working on the command line, you can simply use an alias in place of the entire command you don’t want to type. The alias works exactly like the command it was created for.
In this tutorial, you will learn how to create and use an alias for a command in Ubuntu. We use a simple example to demonstrate this process to you. We will perform the steps and commands described in this article on an Ubuntu 22.04 system.
Example: Setting up an alias for the – sudo apt-get install – command
If you do a lot of installations on your system and wish to avoid using the entire sudo apt-get install command, you can create a short alias for it using the following method:
Through the file manager, open the .bashrc file located in your home folder. This file is usually a hidden file so you will need to use the Ctrl+H control to view all the hidden files located in the folder. You can use your favorite text editor through the command line or UI to edit this file. We are using the default graphical text editor to open and edit this file.
Move to the end of the file and paste the following line:
alias agi='sudo apt-get install'
Here “agi” is the new alias we are setting up.
Syntax for creating an alias:
You can also add other aliases to customize your apt-get commands by adding the following lines to this file:
alias agr='sudo apt-get remove'
alias agu='sudo apt-get update'
alias acs='apt-cache search'
Save the file by clicking the Save button located at the top right corner.
Open the Terminal through Ubuntu Dash or by pressing Ctrl+Alt+T
Run the following command in order to start using the new bashrc file.
$ source ~/.bashrc
The new .bashrc file is installed every time you log out and then log in. The above command enables you to use the changes without restarting your system.
The alias has been set-up; you can now run the following command in order to install a new package to your system:
$ agi [package name]
$ agi nautilus-admin
$ sudo apt-get install nautilus-admin
You can see how in the above image I was able to install Nautilus by using the new command alias that I set up in this example.
Points to consider
While creating an alias please note the following points:
- You cannot use an already existing command as an alias name. If you do this, your alias will not work, instead, the default ubuntu command will be executed.
- The alias name cannot contain any spaces. If the new alias you want to set up contains two or more words, you can use the ‘-’ character to separate those words.
Now you can get rid of the dry and rugged bash commands and use your own customized aliases to run the frequently used operations.