Bash alias is a command-based shortcut title. Every alias comprises a single word (or maybe even a single letter), which can be used rather than a relatively long command. In the Linux system, there have been several instructions that we’ll need to utilize daily. If we can run some popular instructions by typing quick instructions, it would be very beneficial for all of us. Via bash aliases, Linux users can conveniently build commonly used shortcut commands for big commands. Bash aliases are not just used to ease the job and thus save users’ time.

Create Alias in Bash:

Most people prefer to execute commands using shortcuts. You could find yourself, for instance, using the “ls –F” instruction many times. You could even create a detour for this instruction conveniently: for example, “lf”. So when you have to use “lf” in which the shell expects a command, the shell will replace “ls –F”. The alias definition begins with the word “alias”, preceded by a title of the alias, the equivalent symbol, as well as the instruction we intend to execute as we enter the alias. It is appropriate to encapsulate the instruction in quotations and without any spacing all over the equal sign. There is a need to announce each alias on even a new line. It is really easy to construct aliases within Bash. The following is the alias syntax:

$ alias=alias_name=”command_to_run”

Alias Types:

A user may temporarily or permanently claim an alias. It is possible to be using temporary aliases as soon as the user’s access persists. Hence there are two types of alias, temporary and permanent. We’re going to have a look at and analyze both types. Firstly, login from your Linux system and open your command terminal. You will be able to see the already defined default aliases of your Linux system using the simple “alias” command in the terminal, and the list will be displayed as shown below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/1-10.jpg" data-lazy- height="203" src="data:image/svg xml,” width=”721″>

All of these mentioned techniques are carried out on Ubuntu. Conversely, so far, since you’re dealing with Bash, they can function on every Linux distribution.

Temporary Aliases:

So far, because the console session is operating, such a kind of alias persists. It would lose the alias once the shell is ended. Let’s have a look at the temporary alias. Open your command terminal and navigate to the Desktop directory using the below command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/2-11.jpg" data-lazy- height="18" src="data:image/svg xml,” width=”720″>

Perhaps one of the utmost popular instructions on the Linux terminal is the “ls” instruction. Typically, with either the “-la” option, we use this command to display all files and folders, plus secret ones, as in the large list layout.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/3-10.jpg" data-lazy- height="71" src="data:image/svg xml,” width=”718″>

Now using the “ls” command, we will create the alias.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/4-11.jpg" data-lazy- height="20" src="data:image/svg xml,” width=”717″>

The performance of the “L” & “ls -la” instructions may be the same upon constructing aliases.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/5-10.jpg" data-lazy- height="70" src="data:image/svg xml,” width=”722″>

If the window is closed and the consumer begins a new session again, the alias instruction would not operate.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/6-9.jpg" data-lazy- height="37" src="data:image/svg xml,” width=”701″>

Permanent Aliases:

Bash may recall the formation of both the alias as well as its purpose when it is formed. You have to announce it in the .bashrc document to create permanent aliases. The document .bashrc has a bash script that is run each moment a bash process begins. The position is “~/.bashrc”. For every single person in the process, it is special. Let’s have an example of permanent aliases. You can update your system without using the aliases using the update and upgrade command as below.

$ sudo apt update && sudo apt upgrade -y

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/7-7.jpg" data-lazy- height="179" src="data:image/svg xml,” width=”720″>

For making your preferred aliases, .bashrc is indeed a popular approach. Within your setup, .bashrc might not have been active. Create and launch the .bashrc using the nano command. If it is not available, an empty document would be opened.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/8-5.jpg" data-lazy- height="38" src="data:image/svg xml,” width=”718″>

File .bashrc will be opened. Add the below line to the file to make aliases for an update of the system.

alias update=” sudo apt update && sudo apt upgrade –y”

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/9-5.jpg" data-lazy- height="487" src="data:image/svg xml,” width=”713″>

Save the file and close it. After that, run the source instruction in the terminal to replenish the file.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/10-4.jpg" data-lazy- height="36" src="data:image/svg xml,” width=”692″>

This is the moment to verify whether the alias is working or not. Restart the Linux system, get yourself logged in to your Linux system, and execute the alias “update” command that we have just formed. You can see that the alias has been successfully working as it should be and updating the system.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/11-4.jpg" data-lazy- height="207" src="data:image/svg xml,” width=”721″>

Remove Bash Alias:

To remove the formerly formed command aliases, the term unalias is being used. That alias would not function while using this instruction. Well, you may use the unalias instruction to completely disable it if you find that you no longer want to have the shortcut command. Firstly check the already formed aliases in your system using the alias command.

You can see a newly formed alias command “update” is listed in the list below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/12-3.jpg6032c7dae387a.jpg" data-lazy- height="217" src="data:image/svg xml,” width=”723″>

Now execute the “unalias” command to delete the previously made shortcut command.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/13-3.jpg" data-lazy- height="18" src="data:image/svg xml,” width=”718″>

While checking again in the list of aliases, you can see that the “update” alias has been removed completely.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/14-3.jpg" data-lazy- height="201" src="data:image/svg xml,” width=”718″>

You can also erase the aliases from the .bashrc file by opening it using the nano command and deleting it from the file. You can simply comment on the alias line or just remove it completely. After that, run the source command to reflect the changes. Save the updated file and restart your system to check the changes. When you again try the “update” alias command, it will not work.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/15-3.jpg" data-lazy- height="487" src="data:image/svg xml,” width=”713″>

Conclusion:

In this guide, we have studied alias and their two different types. This article is a simple illustration of how to generate an alias as well as execute the commands that are quite often used without typing each instruction over and over yet again. One could now ruminate more about instructions to use far more and generate shortcuts in one’s command shell for them.

About the author

<img alt="Aqsa Yasin" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/Author-Image-150×150.jpg6032c7db0a16e.jpg" height="112" src="data:image/svg xml,” width=”112″>

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.