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!

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Bash uniq Command

Linux users need to create or read the text file in regular basis for many purposes. A text file can contain different...

How to use arrays in Bash

When you want to use multiple data using single variable in any programming language then you have to use array variables....

Copying Files and Copying Directories on Linux

Linux is a place that can do amazing things when performing almost any task. For enjoying the full power of Linux, it’s...