How to Set Up Bash Aliases in Linux

H

If you use the bash command line regularly, you must have noticed that sometimes it’s rather inconvenient and unproductive to type long commands, not to mention that all sorts of options and parameters have to be memorized. Aliases can be used to shorten or rename long commands into shorter ones.

The alias allows a string to be replaced with a single word to simplify a command.

alias [-p] [name[= value] ...]
unalias [-a] [name...]

Notes:
-p; displays the current values
-a; delete all aliases

If arguments are provided, an alias can be defined for each name whose value has been given. If no value is given, the alias command will display the current alias value.
Without arguments or with the -p option, alias displays the list of aliases on the standard output in a form that allows them to be reused as input.
The name cannot be ‘alias‘ or ‘unalias‘.
Unalias‘ can be used to delete each name from a list of defined aliases.

Creating a permanent alias

Use a text editor to create a file called ~ /.bash_aliases, and write all the aliases in this file.

.bash_aliases will run when you log in or can be executed with the command /.bash_aliases.

The first word in every simple, even unofficial command is checked to see if it has an alias. If so, this word is replaced with the alias text.

Alias ​​and text replacement can contain any valid shell input, including shell shells, except that the alias name cannot contain `= ‘.

The first word of the replacement text is tested for pseudonyms, but a word that is identical to an already expanded alias is not expanded a second time.

Alias and unalias are integrated into BASH. For almost any purpose, it is preferable to use shell functions instead of aliases.

Example:

sudo apt-get update && sudo apt-get upgrade

it can be easier to write x and press enter.

We can do this by writing:

alias short_name = “the_command_which_is_replaced”

which can be written as alias x = “sudo apt-get update && sudo apt-get upgrade

After that, we can update the system by typing x and then executing the command. If we want the system to forget what we set up earlier we need to write: unalias name.short and in the case above unalias x.

If you want the settings to be saved even after restarting, you need to create a .bash_aliases text file in your directory /home/username.

Input the desired command into it, for example alias x = “sudo apt-get update && sudo apt-get upgrade” and save.

If we know the alias, but we do not remember exactly what it is for, or we want to see its syntax, it’s enough to type the all-alias command.

To see all aliases made to a specific command we can input: alias | grep command or cat. bashrc | grep command, or grep command.bashrc.

Also note that aliases in /home/username/.bashrc are only valid for the user, if we want aliases for the root account, they will be placed in the /root/.bashrc file, and if we have multiple user accounts, and we want general aliases valid/usable by all users, we add these aliases to the /etc/skel/.bashrc file.

About the author

Ilias spiros
By Ilias spiros

Recent Posts

Archives

Categories