In this tutorial, you will learn how to create a custom alias on Linux. But before we start with the tutorial, let's learn first what alias is on Linux.
Alias basically is like a shortcut that allows you to set a custom word let's say for an x command that has more than 2 words, for example, the update command is sudo apt update
what alias can do is that allows us to execute this command by typing only a custom word in this case update
, so when the execute the word update the alias will execute sudo apt update
To create a custom alias on your system, follow the steps below
Step 1 - Create your alias
Aliases are located in the bashrc
, this file is the file that contains all the configurations for your users shell.
To create the alias open ~/.bashrc
with a text editor
sudo nano ~/.bashrc
Next, you should see this screen, this means the bashrc file has been opened with the text editor you specified on the command above
Alias syntax that you need to use
alias custom-word='custom-command'
In this case, custom-word is the shortcut command you are going to run and custom-command is the command which this shortcut will call. For example, we created an alias for the sudo apt update command, and the alias looks like this
alias update='sudo apt update'
Next, simply write the line above into the bashrc file, and the file should look like this
Make sure the alias you added is not commented, next save changes you made on the bashrc file by pressing CTRL + X and then press Y.
Note: You can also create the alias here ~/.bash_aliases
instead of creating them on the main bashrc file if you are using a debian based system.
If you haven't created an alias before, or if you did you created them only in the bashrc file, then bash_aliases
will be empty when you open it. The procedure of creating aliases in this file is same as bashrc file.
If the alias command doesn't work when you add it on ~/.bash_aliases
make sure to check the main ~/.bashrc
if it's loading ~/.bash_aliases
This part of code checks if bash_aliases
exists and runs it. If you can't find this code on bashrc file, then simply add it
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
Step 2 - Save the alias
By default alias becomes active once you login on your system, if you want to use this alias without logging out of your system, then you can use the source command, source command executes the changes you made on the bashrc file in the current shell
source ~/.bashrc
Step 3 - Run your alias
Now, you can run your alias on your terminal. For example, the alias that we created is called update
so whenever you run update it should update the system
If the alias doesn't work, check the ~/.bashrc file again and make sure you are using the correct alias syntax as we showed on the example above. If the syntax looks correct, then you may need to run the source command again source ~/.bashrc
run this command as is without spaces or any other character and the alias should work.
Conclusion
In this tutorial, you learned how to create an alias and what alias command does on Linux.
If you don't feel comfortable or familiar when running long command in your terminal, then using alias commands can help you.