logo

Practical Computing Advice and Tutorials

Sun: 26 May 2019


Site Content

Programming
&
Development


Technical Knowhow


Command Line Interface


Security

Linux | Alias

The alias command is a useful way of executing a compound of commands by stringing the commands together. As an example, to update a Debian based Linux distro, this set of commands can be used.

:~$ sudo apt-get update
:~$ sudo apt-get upgrade
:~$ sudo apt-get autoremove
:~$ sudo apt-get autoclean

While it's recommended that these commands be memorised, it's a little long-winded to type all of them each time one needs to do a system update. The process can be simplified in a number of ways and each Linux user will have his or her own approach. One way is to create an alias command that executes them all in one.


Creating an Alias command

We'll call this compound command sysupdate and to create it we use this command in the CLI

:~$ alias sysupdate="sudo apt-get update && sudo apt-get upgrade && sudo apt-get autoremove && sudo apt-get autoclean"

You'll notice here that a double ampersand is required between the commands and now, simply entering the command sysupdate will execute all four commands in one go. The reason we do things this way is because we only want the commands to chain together and execute if, and only if, the preceding one returns an exit status of zero. So, if any one of the commands in the chain fails that test, our aliased sysupdate command will halt.

Our new command is only good for the current session, but to make our new command sticky, that is to say to have it available after a reboot or with a new session, we could simply scroll back through the bash history, using the up-arrow key, until we got to the alias assignment, but we can do better than that.

When a new session is started, one of the things that happens is that a standard personal initialisation file (~/.bashrc) is loaded. This file contains shell commands and is typically used to set user preferences such as the CLI prompt, command history length and so forth, as well as setting environment variables. We can see the file by typing cat .bashrc The dot prefix means that it's a hidden file, so you'll not see it with the ls command, but hidden files can be forced to show in a list with the -a switch like this ls -a

We could simply append the .bashrc file with our alias command assignment, but it's possibly a better option to create a new file so that we can keep all of the alias assignments together and in one place. As a side note, if you do accidentally break or delete your .bashrc file, look in the /etc/skel directory for a back-up copy from which you can create a new .bashrc file in your home directory.


Creating a custom .bash_aliases file

First off, make sure that you're at your home directory by simply entering the cd command at the terminal prompt; it should look something like this... rob@HP-6000-server:~$ ...but it's possible that yours may differ. You can check the current, or working, directory with the command pwd which means print working directory. My system shows /home/rob

Now enter the command cat .bashrc and look for an entry like this...

if [ -f ~/.bash_aliases ]; then
  . ~/.bash_aliases
fi

That entry will look for a file called .bash_aliases and, if found, will load it, so that's what we'll need to create and in which we can store this and any other command aliases.

Now use the command nano .bash_aliases This will either create the file or, if it exists, open it for editing. Now drop in this line of code...

alias sysupdate="sudo apt-get update && sudo apt-get upgrade && sudo apt-get autoremove && sudo apt-get autoclean"

Now save the file by holding down the Ctrl key and pressing X, then hit the Y key followed by the Enter key. If all went well, you should be back at the terminal prompt, at which entering the command cat .bash_aliases should display that line of code.

To test our new command, we'll need to reload the .bashrc file with the command source .bashrc after which the newly minted command sysupdate should be working.

You now know enough to create your own alias commands and the potential to get yourself into bother. Make sure that any commands that you create do not conflict with existing commands. A good test, although not fool-proof, is to try the man command with your command name. As an example man sysupdate should return No manual entry for sysupdate whereas man ls should return the manual entry for the ls command, thus you'd be wise not to create an alias command called ls.