What is Bash Alias and how to add one in Ubuntu 20.04

Submitted by foxadmin on Sun, 05/10/2020 - 13:15
Bash Alias in Linux: Ubuntu

Linux commands are cool and fun to work with. Be it ls (to list files) or cd (to change the directory), they are already short and precise. This is one of the reasons we love Linux. However, if you have to repeatedly type a set of commands or a long command with too many parameters, its not fun anymore. The Linux creators were caring enough to provide a solution for that too. The solution for such cases is Bash Shell Alias.  A Bash Shell Alias(often called as a Bash Alias or simply an Alias ) is an alternate name, usually a short one, given to a command or a set of commands. This article shows how to get started with aliases and how to add them permanently in Ubuntu 20.04. It works for older versions too.

For example, I have to repeatedly type  cd /var/www/html in my day-to-day work.  So I want to have a shortcut for it. I can create a custom command, say cdh, which is equivalent of the same.  I can do so by typing the following on the terminal.

# Note: there should NOT be an space between cdh and 'cd /var/www/html'
$ alias cdh='cd /var/www/html'

 

How to add Bash Alias on ubuntu-20.04 - Steps by Foxbits
The cdh command did not exist before. It is created and works after adding the alias.

 

If you are seeing this for the first time, I am sure you found it interesting and are excited to learn more about it. So, lets begin with the basics: the Syntax.

General Syntax:

#syntax for adding an alias to bash shell
alias MY_ALIAS_NAME='My_Command or A Set of Commands'

# To be more specific, here are a few samples
alias MY_ALIAS_NAME='My_Command'
alias MY_ALIAS_NAME='My_Command Argument_1, Argument_2 ...'
alias MY_ALIAS_NAME='My_Command_1; My_Command_2'
alias MY_ALIAS_NAME='/PATH/TO/A/SCRIPT'

 

Lets see another example now. A very commonly used ls command is supplied a few options so that it lists the latest 10 files and their description in human readable format.

# ls (to list files)
# -l (list in the long format)
# -h (show in human readable form)
# -t (sort by modified time, the latest first)
# head -5 (show only 5 rows/items)

$ alias lsrt='ls -lht | head -5'

 

Alias for ls command  to list the latest 5 files
Alias for ls command  to list the latest 4 files

 

This looks fine but it would be better if we can make that number 5 dynamic. I mean wouldn't it be much better if we can have something like lsrt 10 that would list 10 rows and lsrt 15 that would list 15 rows. We are going to do that soon. Before getting into that, lets talk about the lifespan of these aliases. 

Temporary Vs Permanent Bash Aliases

The method we have seen so far adds the Bash Aliases temporarily. As soon as you close the Terminal window, the aliases are gone from the computer's memory. In your new session, you will have to define them again to use them. Not cool right? So lets see how to make them permanent.

 

Making the Bash Alias Permanent

Its easy, I assure you. To make it permanent in Ubuntu 20.04, you will have to save those Aliases in your ~/.bash_aliases file.  Lets do it step by step. Before starting, two things to note here. First, the ~/ denotes the location of the.bash_aliases file. ~ (pronounced as tilde) is a short notation for the current user's home folder(/home/username) in Linux systems. Second, the .(dot) at the beginning of the filename denotes it will be a hidden file.

Step 1: 

Using nano or vim or any editor of your choice, create a .bash_aliases file at the location ~/. Then add the following lines to it. If the file exists already, then append the same at the end of the file. In many tutorials online, you can see people suggesting to add the aliases in your ~/.bashrc file directly. While this is totally possible and gives us the same result, using ~/.bash_aliases is the recommended way. It prevents us from messing the ~/.bashrc file. If you want to do it for some reason, you can. Just make sure you take a backup of the ~/.bashrc file before making any changes to it so that in case anything goes wrong, you can restore it.  

alias cdh='cd /var/www/html'
alias lsrt='ls -lht | head -5'
Bash Aliases added to Ubuntu 20.04
Bash Aliases added to ~/.bash_aliases file in Ubuntu 20.04

 

Step 2:

After saving the file, do the following in the Terminal.

Source .bash_aliases file Ubuntu 20.04
source ~/.bash_aliases file Ubuntu 20.04

Congratulations, you have now added the Bash Aliases permanently in your system! They will be available to you even when you reboot the system. Now lets get back to the promise I made earlier, about making our custom command lsrt dynamic. To make it dynamic, its obvious that the numerical value 5 that we have in our command will have to replaced with a variable. Here's how we do it.

Replace the alias we added earlier with this one:

alias lsrt='list_sorted(){ ls -lht | head -$1; }; list_sorted'

What has changed?

There are two major changes done for lsrt here.

  1. The command has been moved to a shell function list_sorted(). This function is called immediately after its definition.
  2. The hard-coded value 5 has been replaced with $1. The $1 stores the first parameter that we pass while we execute the lsrt command.

 

Your ~/.bash_aliases file should now look like this:

Bash Aliases File Updated
Bash Aliases(~/.bash_aliases) File Updated

Lets see some action again. Run lsrt 5 on your terminal. This should output 5 latest files in your folder.

Latest 5 files with Bash Alias
Latest 5 files with Bash Alias 

 

I hope you can now create your own aliases. Lets see one more.

#this will alias two commands mkdir and cd to one mkcd command
alias mkcd='mkdircd(){ mkdir $1; cd $1; }; mkdircd'

With this one, I can run a single command for mkdir and cd. I frequently create a directory with mkdir and the go to that directory with cd command. This alias (mkcd) will allow me to do the both in a single step, as seen in the image below.

mkdir and cd with single command
mkdir and cd with single command

 

Here is the current screenshot of my ~/.bash_aliases file. It has two extra aliases, which I use on my day-to-day work.

bash aliases file screenshot
Bash Aliases file screenshot

 

Removing the Alias

If you want to get rid of the alias for some reason, you can do it the following ways.

If you have added the alias temporarily, you can remove it just by closing the terminal session. If you don't want to close the terminal, you can use unalias command. Just type unalias Your_Alias_Name on the terminal.

If you have added it permanently, then you can edit ~/.bash_aliases file and remove the related alias line.

 

Summary

  1. An alias is a customized short cut command in Linux.
  2. It can be added temporarily by using alias command.
  3. It can be added permanently by inserting alias to the the ~/.bash_aliases file and running source command.
  4. Aliases make our life easier.

 

What are some of the coolest aliases you have used/seen? I would love to see your ideas for interesting aliases in the comments.

Comments