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
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.
In essence, 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.
alias [-p] [name[=value] ...]
unalias [-a] [name ... ]
-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.
Create an alias 'c' that will clear the screen:
$ alias c='clear'
Using An Alias
Alias, as we use with Bash can be viewed by entering the following into the Terminal:
Once Enter is pressed, you'll be presented with a list of currently used aliases. In the current version of Linux Mint, 16, we have:
alias grep='grep --colour=auto'Let's start with something simple. The pwd command, when entered into a Terminal, displays the current location of the user in the system directory structure; it stands for Present Working Directory. If we wanted to, instead of typing in 'pwd', to create an alias for this, then we could opt for simply entering p.
alias Is='ls --colour=auto'
If you were to enter the letter p into the Terminal, the chances are you'd get the 'command not found' response back. So if you now enter the following:
alias p="pwd"Now press Enter. The next time you enter just p you'll get the same command as if you entered pwd. Moving on a little, if we were to use the example mentioned earlier, the sudo apt get install command, we can change it to the following:
alias install= "sudo apt-get install"After pressing Enter the alias should now be set, and if we wanted to install a specific package, such as Gimp for example, then all we'd need to enter (after setting the alias) from then on is install Gimp. You will still need to enter your password, but the command will be issued as it would if you typed it in normally.
alias poweroff="sudo shutdown -h now"As you imagine these three examples can be tweaked to your particular liking. And you can also include a more complex command in the alias, so where update and upgrade do different commands, you can combine it to one. For example:
alias update="sudo apt-get update"
alias upgrade=" sudo apt-get upgrade"
alias sysupgrade="sudo apt-get update && sudo apt-get upgrade"It can get a little messy like this, though. Thankfully, there is a way to chain the aliases together. Chaining Aliases Let's say we set the update and upgrade alias to both sudo apt-get update and sudo apt-get upgrade individually. Rather than creating another messy alias we could simply chain the aliases together so you can enter:
update && upgradeThe result is exactly the same as if you entered the commands in their original format hence the need for the double ampersand (&&) to combine two separate commands.