Tuesday 22 January 2008

Lets speak some Linux: How to make an alias of a command

The first thing to mention is that almost all of my Linux experience comes from the realm of Ubuntu. This is very nice system. It appeared little strange to me the first time, because my first Linux was SuSE 9.3 ... but I'm gettin' off the topic (it's gonna be chaos I told you). About distributions I'll talk later.

So. Ubuntu. No root password, still nifty super-user access.
Now the aliases. Simply put the alias is just a Linux command. It is a way to define a replacement for other Linux command. Generally one uses it to ease their lives by replacing a more complex command (with usually a big set of parameters) with a much shorter representation.

If you google for the 'alias linux' a lots of nice and detailed resources will come up.
Nevertheless in a nutshell my comprehension of the subject - there are two basic ways for defining an alias:

* In a file. When logged in Ubuntu as a regular user in your environment (e.g. your user's home directory) there is a hidden file called .bashrc. This is a configuration file for your bash shell, so everything defined here applies to the terminal window you use with your account (if you sudo
for example, the scope will change, and different configuration is applied). If you scroll down the file to line around 60-70 you will stumble upon lines which define some quick aliases. These are just simple examples but you can get the point. The syntax is:
$ alias alias_name='regular_linux_commad -with -options'
or just
$ alias ecl='/home/user/eclipse/eclipse'
if I want to run my eclipse IDE from the command line the fast way.

* On the console or inline. That's the quicker way - you just issue the command and the shell remembers it. Quicker but dirtier because the shell remembers your alias only for the current session. So if you want your alias to last for good (or at least for the lifetime of your Linux distribution or /home partition) after being defined only once, you'd better use the first way.
A thing about the above example - in aliases where you specify a path on a partition might appear traitorous, especially if you write relative paths in your alias - they become dependent on where you are at the moment. So if you ever use path in an alias, just make it absolute. Avoid the relative ones.
To summarize - the alias command defines aliases to complex commands and it's purpose is to simplify your experience with the shell. Put in other way the alias might be thought of as one-line shell script. That's a funny approach :D

That's for now. See you ;)

No comments: