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

74 Bash Operators Examples

Different types of operators exist in Bash to perform various operations using bash script. Some common groups of bash...
29/12/2020

Find Length of String in Bash

The total number of characters of any string data indicates the length of the string. When we work with string data then...
29/12/2020

Bash brace expansion

Bash uses brace expansion to generate a sequence of strings from the terminal or by using any bash script. A sequence of...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Mua proxy v4 chạy socks5 để chơi game an toàn, tốc độ cao ở đâu?
18/05/2024

Thuê mua proxy Telegram trọn gói, tốc độ cao, giá siêu hời
18/05/2024

Thuê mua proxy Viettel ở đâu uy tín, chất lượng và giá tốt? 
14/05/2024

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024