How to Create Bash Aliases?

Sometimes you need to type several commands repeatedly to perform a specific function, which is time taking. To avoid writing long codes, the bash aliases are used. The bash aliases act as a shortcut for performing several tasks simultaneously using a single code line.

These bash aliases are saved in the “~/.bashrc” file, which can be invoked by writing the shortcut name for the specific alias.

This guide will explain the methods to create the bash aliases in Linux, with the following timeline:

What are Bash Aliases?

The bash aliases are a simplified way to run multiple commands using shortcuts. There are several built-in default aliases in the bash. To get all the bash aliases currently in the system, use the following command:

$ alias

The above aliases already exist in the system.

General Syntax

The general syntax for creating the bash alias is given below:

Add the bash aliases when you want to create whose syntax is:

alias <alias-name>="commands"

You can create a bash alias using the function as well, whose syntax is as follows:

function <alias-name> ()
{
   <commands>
}

Method 1: Create a Bash alias Directly in ~/.bashrc File

The below steps to create bash aliases directly from the ~/.bashrc file:

Step 1: Add the Alias to the “.bashrc” File

Open the ~/.bashrc or ~/.bash_aliases file using your desired editor:

$ sudo nano ~/.bashrc

For instance, to print the hostname and your user name, you can create the below aliases for both commands:

$ alias hostuser = "hostname && whoami"

After adding the desired bash aliases, save the file and quit the editor.

Alternate Way of Step 1:

Another approach can be used to perform the above method as well:

To add the bash alias directly to the “~/.bashrc” file, use the below command:

$ echo 'alias hostuser="hostname && whoami"' >> ~/.bashrc

Step 2: Read and Execute the .bashrc Content

To save the changes permanently in the ~/.bashrc file, use the below source command:

$ source ~/.bashrc

Step 3: Test the Alias From the Terminal

Now, you can permanently use the “hostuser” alias. To run the bash alias created, enter the alias name. All the commands for that alias will automatically run:

$ hostuser

Example: Create Bash Alias for Update and Upgrade

Before installing the packages, we usually update and upgrade the system to get & install the latest version for that package. Instead of writing both commands every time, we can create its alias as shown below:

$ alias UpdateUpgrade="sudo apt update && sudo apt upgrade"

Add the alias at the end of the .bashrc file by opening it in any editor (in this case, nano):

$ sudo nano ~/.bashrc

Press “Ctrl + O” to save the file. Then press the “Enter” to keep the file name the same and press the “Ctrl + X” to exit.

To save the changes permanently in the ~/.bashrc file, use this source command:

$ source ~/.bashrc

To verify the alias is created, let’s run it:

$ UpdateUpgrade

The packages are updated and upgraded using the alias. 

Method 2: Create Alias Using a Separate ~/.bash_aliases File

The steps to create a separate ~/.bash_aliases file (where the aliases will be created) and add it to ~.bashrc file can be done by performing these steps:

Open the ~/.bash_aliases file in any editor (nano used in this guide) with this command:

$ sudo nano ~/.bash_aliases

Add the alias code in ~/.bash_aliases file and save the file.

Open the ~/.bashrc file and add the below code to run the bash_aliases file automatically every time the specific alias is called from the ~/.bash_aliases file:

Note: The below code refers to the bash_aliases file in the .bashrc file.

$ if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

Save the ~/.bashrc file and exit the editor.

To save the changes permanently in the ~/.bashrc file, use the following command:

$ source ~/.bashrc

Example: Create Bash Alias for Listing the Files Modified Time

This section will create a bash alias for the “ls” command, which will provide the long list (l) and sort in reverse order(r) according to modified time (t) using a separate file ~/.bash_aliases, which will be done by performing the following steps:

Open the ~/.bash_aliases file and add the desired alias command in it:

$ sudo nano ~/.bash_aliases

Save the file and exit the editor.

Open the ~/.bashrc file using “sudo nano ~./bashrc” and add the code below to refer the bash_aliases file to the bashrc file:

$ if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

Save the changes and exit the editor.

To save the alias permanently, use the following command:

$ source ~/.bashrc

The alias is permanently saved in the ~/.bashrc file, now run the “ltr” alias: 

$ ltr

The output shows that the “ltr” alias is created, and it shows the long listing sorted in reverse order according to the modified time, for which we created this alias.

Bonus Tip: How to Remove Aliases?

The unalias command is used to remove the already created aliases. To create a specific alias from the system, use:

$ unalias <alias-name>

To remove all (a) the aliases that exist in the system, use the below command:

$ unalias -a

That’s the end of this guide.

Conclusion

The bash alias helps users to create a shortcut for running several commands simultaneously. We can create bash aliases by directly adding the desired alias into the ~/.bashrc file or creating a separate file for aliases and adding its reference to the ~/.bashrc file, as discussed in this guide.