The Linux alias command makes it possible to create shortcuts and to use relatively long commands more quickly. The alias command for Linux hangs every time you close the terminal unless you create this command permanently. To undo it manually, you can also use the unalias command.
Linux command alias: what is it?¶
There are bound to be some Linux commands that you use more often than others. If these commands are relatively long or correspond to a whole set of command strings, you must type them completely each time. This can be complicated and time-consuming, with a high risk of error. The Linux alias command allows you to avoid these problems: if you set it up for the first time, it can then act as a shortcut to a longer command. A few characters are enough to create an alias command, which makes typing much faster and easier.
However, the Linux alias command remains ephemeral: indeed, it is automatically suspended by default each time you close the console or log out. We will also show you how to bypass this restriction. To manually undo an alias command for Linux, you can use the unalias command. It is also possible to preview all Linux alias commands in use. To do this, run the following command:
How does the alias command work?¶
The alias command allows you to create a shortcut to a longer command, or even to a chain of commands. For this purpose you can choose for each longer command the alias command to be used. The system will thus be able toexecute the command thus defined, even if you only type the abbreviation. As already mentioned, however, this procedure only applies for the duration of your session. Once it is finished, the system is responsible for canceling this command.
What does the alias command syntax look like?¶
Below is the syntax of the Linux alias command:
$ alias Abréviation="La commande à laquelle vous souhaitez attribuer un alias"
shell
In this example, « alias » represents the command, while « Abbreviation » represents the shortcut that you are free to specify. After the “=” sign, you just have to enter the command for which you want to define this command alias.
For reference, below is a practical example of using the Linux alias command:
Now, if you want to clear all screen content in the terminal, you just need to use the “c” shortcut.
What are the Linux alias command options?¶
Two options are available to you.
- –p: this option displays all alias commands in the terminal, so that you can reuse them immediately.
- –help: this option displays all available helpers for the alias command.
How to use alias commands permanently?¶
To use an alias command with Linux permanently, you have two options, both of which require nothing more than an editor.
With “.bash_aliases”¶
- Create a file called ~/.bash_aliases using the editor of your choice.
- Then list all the alias commands you want to use.
- Save the file, then close it.
- Now open the file .bashrc and add the following lines to it:
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
shell
- Now all you have to do is read the alias command file:
source ~/.bash_aliases
shell
With “.bashrc”¶
- Open the file ~/.bashrc using your editor.
- Navigate to the “Alias definitions” section.
- Create the desired alias commands there by filling in the corresponding shortcuts.
- Save the file, close it, then start a new session.
- Read the configuration file again. To do this, use the following command:
Linux alias command: some examples¶
In principle, you can use the Linux alias command for a variety of commands. It can be useful in particular in configurations similar to the examples below, which otherwise involve a high risk of error:
$ alias ll="ls | less"
shell
This Linux command alias allows the ls command to be passed to « less » and provides you with longer outputs on a single page.
$ alias up="sudo apt update && sudo apt upgrade"
shell
The two commands for updating packages are combined here.
$ alias des="cd ~/Desktop"
shell
This alias command for Linux gives you direct access to your desktop.