An alias is effectively a “shortcut” command in Bash. For example, if you use the terminal in Ubuntu a lot, and get sick of typing
sudo apt update;sudo apt upgrade -y all the time, you can create an alias called “update” that will execute those same commands without needing to type out so much.
Bash aliases are defined in every user’s home directory in the .bashrc file. For more information on aliases, run the man command below. It will show you all there is to know about aliases, how they work, and what you can do with them.
man bash | grep alias
You can also take a look at the complete Bash manual by running:
Setting commands as aliases
The most common use for alias in Bash is using it to execute multiple, long commands at once to save time. In this example, we will go over how to turn Ubuntu’s long update commands into a simple alias.
To start the process, open up a terminal window on your Linux desktop by pressing Ctrl + Alt + T or Ctrl + Shift + T on the keyboard. Then, open up your
.bashrc file in the Nano text editor with the command below.
nano -w ~/.bashrc
Inside of the
.bashrc file, you will notice a lot of code already there. The developers of your Linux distribution define this text. If you do not understand what any of it is, it is best to ignore it and leave it be, and move to the bottom of the file.
At the bottom of the .bashrc file, press the Enter key to create a new line. It is critical to create a new line in your .bashrc file before adding to it, as you could mess up the code already there.
After creating a new line in the .bashrc file, write out
alias on the new line. Each new alias you create must start with
alias , otherwise .bashrc and your terminal emulator will not read it correctly.
alias= on the new line, you must give your new alias a name. In this example, we are covering Ubuntu’s long update commands, so, we will use ubuntu-update as the new alias name. However, feel free to name your alias whatever you like.
Once you’ve named your alias, it is time to add in the
Next, after the
= sign, add in the first
" (quotation mark). This quotation mark will contain all of your commands within the alias.
So far we have
alias ubuntu-update=". Now it is time to add in the commands that we want the alias to call when ubuntu-update is used.
; to write multiple commands in one line. Such as
command1;command2;command3, and so on.
alias ubuntu-update="sudo apt update;sudo apt upgrade -y
When you’ve finished writing out the commands in the alias, close it off with the second
" (quotation mark). With both quotation marks, it should look like the example below.
alias ubuntu-update="sudo apt update;sudo apt upgrade -y"
Save your new alias by pressing Ctrl + O on the keyboard. Exit Nano with Ctrl + X. Then, close the terminal window and re-open it.
When you’ve re-opened the terminal, run ubuntu-update to try out the new alias.
Setting bash scripts as aliases
Did you know that it is also possible to run bash scripts as an alias? Here’s how it works.
First, open up your
.bashrc file. Then, go to the bottom of the file and press the Enter key on the keyboard to create a new line in the file.
sudo nano -w ~/.bashrc
On the new line, write alias followed by the name of the script. In this example, the script name is mybashscript. It should look like
Next, add in the
= sign, and the first
" (quotation mark). After adding in the
", it will look like the example below.
Following the first
" (quotation mark), add in the command to launch your code. Please remember that this is an example, so you will need to replace
~/path/to/bash/script/script.sh with the actual script file, you want to execute within the
alias mybashscript="bash ~/path/to/bash/script/script.sh
After writing in the command to execute the script file, close off the alias with the second quotation mark
". When the entire alias is written out, it should look something like the example below.
alias mybashscript="bash ~/path/to/bash/script/script.sh"
Save the edits to the .bashrc file by pressing Ctrl + O, and exit with Ctrl + X. Then, close your terminal window and re-open it to execute your new bash script via an alias.
To launch the alias, run mybashscript (or whatever you’ve named your alias) and press Enter.