Last Updated on May 2, 2020 by admin
Linux Command Alias
Most Linux users are well versed in the typing out of lengthy commands in the Terminal. They’re especially hardened and generally insured against such sudden lapses in concentration, where they have to go back mid-command to correct an error.
The rest of us, however, have to deal with the fact that an error is inevitable and that we more often than not have to enter the command again to get it working correctly. There is another option available for Linux users, though. Instead of constantly or frequently having to tap out the complex line of instructions, we can instead use an Alias.
The Alias command isn’t just for Linux; there are examples of it being used via the DOS Key command in the past with DOS and earlier versions of Windows, and more recently appearing in the Windows PowerShell. However, it was introduced back in the old UNIX days and made life significantly simpler for the users of the time.
What is an alias in linux?
The Alias command can be issued to replace a common command with a shorter word. So where in Linux the command ‘sudo apt-get install’ needs to be inputted, an alias can be set up so all the user has to do is type in ‘install’, for example. This will then last for as long as the user is in the Terminal for, but they can be set as permanent, which we’ll get into later on.
Create an alias, aliases allow a string to be substituted for a word when it is used as the first word of a simple command.
Syntax alias [-p] [name[=value] …] unalias [-a] [name … ] Key -p Print the current values -a Remove All aliases
If arguments are supplied, an alias is defined for each name whose value is given.
If no value is given, alias will print the current value of the alias.
Without arguments or with the -p option, alias prints the list of aliases on the standard output in a form that allows them to be reused as input.
The value cannot contain any positional parameters ($1 etc), if you need to do that use a shell function instead.
The name may not be ‘alias’ or ‘unalias’.
unalias may be used to remove each name from the list of defined aliases.
Make an alias permanent
Use your favorite text editor to create a file called /.bash_aliases, and type the alias commands into the file.
.bash_aliases will run at login (or you can just execute it with ..bash_aliases )
Expand Multiple aliases
If the last character of the alias value is a space or tab character, then the next command word following the alias is also checked for alias expansion.
Examples: Create an alias ‘c’ that will clear the screen:
Syntax of alias command in linux
How to create alias in linux
In general, the creation of alias is as follows:
alias name=value alias name='command' alias name='command arg1 arg2' alias name='/path/to/script' alias name='/path/to/script.pl arg1'
That is, we write the name of the alias, put the “=” sign, then indicate the command or list of commands in quotation marks with arguments. Instead of console commands, we can specify the path to the script with or without arguments. Let’s look at examples.
In order to completely update the system, we need to enter the commands:
sudo apt update && sudo apt full-upgrade
Each time to write this for a long time, we can create an alias:
alias upd='sudo apt update && sudo apt full-upgrade'
Let’s redefine the ping command by setting the parameter so that only 4 packets are sent when it is used:
alias ping='ping -c 4'
How to invoke a command that has been overridden by alias.
But what if we want to use the old ping command? This can also be done, you need to enter the backslash “” before the command:
In addition, in addition to the backslash, there are other ways to call the original command:
"ping" 'ping' command ping
That is, by enclosing in quotation marks or by entering the keyword “command” before the command.
Removing alias from the system
The unalias command and the alias name are used for deletion.linux remove alias In general terms, it looks:
For example, I mistakenly added alias: X and i want to remove it i would type this command
and if you want to delete all aliases; for this, use the command:
unalias -aThat is, with the -a option .
Note:After closing the terminal, all our aliases (aliases, aliases) are deleted, except for system ones. That is, these aliases exist only within the framework of the current terminal session.
If you added alias in one tab (window) of the terminal on another tab (in another terminal session), the system knows nothing about this alias but this can be solved by creating permanent aliases.
how to create a permanent alias in linux
permanent alias linux I’ll go from afar, in the user’s home directory there is a file .bashrc This file is used in the system when this user is authorized. That is, our login. There are lines in this file:
# Alias definitions. You may want to put all your additions into a separate file like ~/.bash_aliases, instead of adding them here directly. See /usr/share/doc/bash-doc/examples in the bash-doc package. if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases fi
This means that if the .bash_aliases file exists, then it is used in the system. We will create this file and write to it a list of our aliases so that they are always available. But unlike Kali Linux or Ubuntu, for example, in Linux Mint this entry may be missing, so add these lines to your .bashrc at the end of the file.
In order not to restart the system and the changes to this file are applied, we execute the command:
sudo mousepad .bashrc
We figured it out. Let’s create a ~/bash_aliases file. It can be created in the file manager, superuser rights are not required. Or we enter in the terminal:
In this file, insert the list of aliases that we consider necessary:
Save the file, If everything was done correctly, now these aliases will be available in any terminal window of the current user. And also after reboot will be available.
This article has been detailing the creation of alias in Kali linux. If you use this convenient tool, you can share your set of aliases in the comments.
For more linux command check Linux basic commands