Bash Alias Tutorial

28/12/2020

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 Script User Input

Taking input from the user is a common task for any programming language. You can take input from a user in bash script...
28/12/2020

Bash Environment Variables Tutorial

Overview Whenever we talk to a remote server or do anything on a shell, the shell stores data in form of variables which...
28/12/2020

Cài đặt Prometheus trên CentOS 7

Prometheus là một trong số các công cụ dùng để giám sát rất hữu ích hiện nay. Dưới đây sẽ là...
28/09/2021