BASH Programming

Bash Alias Tutorial

How to use bash aliases

Most of the users like to use shortcuts for running commands. There are many commands in Ubuntu that we need to use regularly. It will be very helpful for us if we are able to run those common commands by typing shortcut commands. Using bash aliases, Ubuntu users can easily create shortcut commands of the large commands those are used frequently. Bash aliases not only makes task easier but also save the time of the users. The user can declare alias temporary or permanently. The temporary aliases can be used as long as the session of the user exists. If the user wants to use shortcut commands every time the session starts then he or she has to create permanent alias by using ~/.bashrc and ~/.bash_profile files. How you can create and use bash aliases in Ubuntu is shown in this tutorial by using some examples.

Example-1: Temporary bash alias declaration

Declaring a temporary bash alias is a very simple task. alias command is used to create a shortcut of any command. For example, ls -la command is a very commonly used command to display the list of all files and folder with permission. You can create the shortcut of this command by using alias command. Suppose, the shortcut command will be L. Run the following commands to create the shortcut of ls -la and test the command.

Check the output of ls -ls command

$ ls -ls

Create alias command for ls -la

$ alias L=”ls la”

Test the shortcut of ls -ls

$ L

After creating alias, the output of “L” and “ls -la” commands will be same.

If the user close the session and again start new session then alias command will not work.

Example-2: Permanent bash alias declaration

To solve the above problem you can create permanent alias command. Suppose you want to create a shortcut of “mkdir” command with alias name “C” and use it permanently. Open ~/.bashrc file in any editor, add alias command in that file, save the file and run command to re-execute the file with added alias command.

Open the file in nano editor.

$ nano ~/.bashrc

Add the alias command in the file and save it.

alias C=”mkdir

Re-execute the file to make the command active.

$ source ~/.bashrc

Test the shortcut command of “mkdir”

$ C TestDir

Example-3: Remove bash alias

unalias command is used to remove the previously created alias command. After using this command the alias will not work. So, if you think you don’t want the shortcut command anymore then you can use alias command to remove it permanently.

Run unalias command to remove previously created shortcut command.

$ unalias L

Check the command is working or not.

$ L

Remove or comment the line that is used for creating alias command from ~/.bashrc file and re-execute it for deleting permanent alias.

You can use alias command on various purposes for creating shortcut of the commands. This tutorial will help to understand the basic use of alias command, so that you can easily apply this command for creating shortcut of your regularly used commands. For more information please watch the video!

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.