An alias is another name for a command. You can use the alias command to create, change, and manage aliases on Linux. By default, the alias command creates and updates aliases in the current session. To manage them permanently, you need to modify configuration files.
The alias command
The alias command creates an alias for an existing command. You can use an alias as the substitute for the command. For example, if you use a long command frequently, you can create a short alias for that command. After creating an alias, you can use the created alias instead of the command. This way, aliases improve efficiency and avoid potential spelling errors.
The alias command syntax
The alias command uses the following syntax.
alias alias_name='value'
alias:- This is the main command.
alias_name:- It defines the new alias name for the actual command. It can be any user-defined string, excluding special characters and the 'alias' and 'unalias'. The words alias and unalias are used as the main command.
value:- It specifies the actual command the alias references. The command can also include options, arguments, and variables.
Printing the currently configured aliases
Linux automatically creates aliases for frequently used commands. To view the default aliases, use the following command.
#alias
Creating aliases
There are two ways to create aliases: temporary and permanent. A temporary alias works only in the current session. Linux automatically deletes it when the user logout from the current session or shutdown the system. A permanent alias remains available until you manually delete it.
Creating and managing temporary aliases
The following command creates an alias in the current session only.
#alias alias_name='actual_command'
The following command creates an alias command lh for the actual command 'ls -l /root'.
#alias lh='ls -l /root'
Deleting or removing temporary aliases
Linux automatically removes a temporary alias when you exit the current session or restart the system. But, if you want to delete it immediately, you can use the unalias command.
The following command deletes the lh alias.
Creating/deleting permanent alias
To create a permanent alias, you need to add the alias in the .bashrc file. The shell reads this file when the user login.
Open this file.
Add your alias at the end of the alias section and save the file.
Logout from the current session.
The custom alias will be available from the next session.
To delete a permanent alias, open the .bashrc file, removes the alias you want to delete, save the file, exit the current session, and log in again.