Working With Bash Aliases In Linux
One of the really handy features of the Bourne Again SHell (bash) is that it has the ability to use aliases. Using bash aliases can make working at the command line a lot easier by shortening long commands to something more memorable, or by adjusting the operation of commands by injecting flags.
How do aliases work?
When you enter a command at the command line, bash will start by checking your aliases for any matches before looking for matching commands. If there is an alias match, the matching part of the command is replaced by the replacement in the given alias. This could be just a command, a command with some flags or even many commands piped together.
Bash Alias Commands
Aliases are managed with the alias command. To view a list of the configured aliases you can use it on its own as follows:
alias
To set an alias you can use it as follows:
alias myalias=’command here’
No spaces can be used when defining the name of the alias, which in this case we’ve used myalias. This should be followed directly by an equals symbol and then the command to be executed within quote marks. Spaces can be used within the quote marks when defining the command.
When the time comes that you no longer require the configured alias then it can easily be removed with the unalias command.
unalias myalias
It is possible to create aliases with the same name as an existing command, and when used the alias will take precedence over the original command. If you do this and later want to use the original command without removing the alias completely, you can prefix the command with a backslash “\” symbol. For example, if you created an alias called ping, then to use the normal ping command would be like this:
\ping
Aliases are only valid for the duration of the session that they were set in. So if you create some aliases at the command line, then they will be deleted when you log out from your session. In order to make the aliases permanent, they would need to be added to a script that is run when you log into the system. A commonplace would be the ~/.bashrc file which we looked at in a previous article.
Helpful bash alias examples
Now that we’ve covered using alias commands, we can now look at some useful alias examples. First, if you come from a Windows background and are familiar with the command line, you may often find yourself typing “dir” to look at the contents of a directory:
alias dir=’ls -l’
With this alias, every time you type dir you’ll get the long format output from ls to show the files in the directory in a manner similar to the Windows dir command. Next, we have another useful alias for Windows users:
alias cd..=’cd ..’
This alias maps allows you to use the command cd.. to go up a directory. Normally on Linux, this would throw a “command not found” error, even though this is valid syntax in Windows.
Next, we’ll look at something a touch more complicated:
alias histgrep=”history | grep”
This alias can be used to search for things in your bash history. While it only saves typing a few characters, it serves as a good example. When you use an alias at the start of the command, you type is replaced using the alias and the rest of the command is kept in place. So for example, the following command “histgrep ps” would become “history | grep ps”.
Next, we’ll step it up a notch:
alias psgrep=”ps aux | grep -v grep | grep -i -e VSZ -e”
This command takes the output of the “ps aux” command and pipes it through the first grep command to remove any entries relating to grep. The second grep command performs a case-insensitive search for lines containing “VSZ,” which will likely only match on the column headers from ps and whatever string was given after the psgrep command. A usage example would be:
psgrep apache
This would give a list of running apache processes.
As you can see aliases can be powerful tools to make your systems easier to use and manage. You can make aliases to correct commonly made typos, abbreviate longer or more complicated commands, or even just make some commands more memorable.