Introduction to Linux Alias Command
In the Linux operating system, the alias command is used to convert any Linux command into own command or own keyword. Whenever we are using the same command number of time in Linux shell then we can assign one keyword to the same repeating command. We can use the keyword instead of the earlier command. By default, the alias functionality comes with the Linux operating system. There is no need to add any extra packages to access or use this utility.
Note: We can use the alias to hide the command identity.
Syntax of Alias Command
Syntax of alias command is given below:
alias [OPTION...] [ ALIASNAME ]= 'LINUX COMMAND'
- Alias: The alias keyword in the command | syntax. It will take two or three sets off an argument as an option or alias name and the Linux command. Accordingly, it will convert the Linux command in alias/keyword.
- OPTION: We can provide the different flags as options that are compatible with the Alias command.
- ALIAS NAME: The name of the alias
- LINUX COMMAND: The name of the Linux command.
How does Linux Alias Command Work?
In the Linux environment, we are using a few command number of time. What would happen if the command length is too big and we need to write the command number of time in Linux shell? Now we need a solution in such a way that there is no need to write the whole command. We just need to add the keyword in the shell prompt and the same keyword will refer as the whole big command.
In another way, the alias command is also suitable for hiding the real Linux command. The same alias will also be useful in the application development task and shell jobs.
In alias command, the major role will play by the alias name and the Linux command. The options parameter will be optional for the command. The command will accept the alias name and Linux command. It will update the alias keyword in the alias Meta information.
If we need to do permanent changes in the system. We need to update the alias entry in the “.bashrc” file of the user.
Examples to Implement Linux Alias Command
Following are the examples of Linux alias command are given below:
Example #1 – AliasCommand
The alias command is useful to get the alias information that is currently available in the system.
Note: It will display both the temporary and permanent alias. If the server will reboot or the session was closed then the temporary alias will not display in the next login.
Explanation: As discussed earlier, we can use the alias to define our own command or command shortcut in the Linux ecosystem. As per the above command, we can list down the number of aliases available in the system. As per the below output (refer Screenshot 1 (a)), these are the default alias available in the operating system. We have created a temporary alias in the system (refer Screenshot 1 (b)). As updated earlier, the alias is temporary if we will not hard code it. After rebooting the server or session close then the alias entry will not present in the system (refer Screenshot 1 (c)).
Example #2 – Permanent Alias
In the Linux ecosystem, we are having the functionality to create permanent alias. As the name suggested, the permanent alias will not remove if the machine or server will reboot or the session will be closed.
alias log='ll /var/log/'
As per the above alias command, we are able to create permanent alias in the system. For creating a permanent alias, we need to follow a few steps.
- First, we need to create an alias in the machine. As per the screenshot 2 (a), we have created the “log” alias and assign the command to it “ll /var/log/”. Before creating the alias, if we will enter the “log” then it will display “command not fount”. After creating an alias, the “log” alias is working fine.
- Second, we need to do the entry of alias in “.bashrc” file (refer Screenshot 2 (b)). It will hardcode the entry to alias and it will not remove in the future.
Example #3 – Temporary Alias
As the name suggested, we can create temporary alias for the temporary work. Once the work will complete then the alias will also vanish from the environment.
alias www='ll /var/www/'
Explanation: When we are working on some development work or doing something. So instead of writing a complete query or command, we just need to write the keyword and the keyword will work as the command. The keyword will not live a long time; once the session was closed the keyword entry will also be lost.
The same thing we can achieve via temporary alias. As per the above command, we have created the temporary alias as “www”.
Example #4 – Unalias
In alias, we are having the functionality to make the alias inactive. In other words, we can delete the alias from the system.
Explanation: As per the above command, we are removing the alias “www” from the system. Previously, the “www” alias will display the output. After removing or unalias the “www”, the output will not display.
We have seen the uncut concept of “Linux Alias Command” with the proper example, explanation, and command with different outputs. The alias command is very important for command translation in the system. It is highly used in application development and shell job.
This is a guide to Linux Alias Command. Here we also discuss the introduction and how does Linux alias command work along with different examples and its code implementation. You may also have a look at the following articles to learn more –