Monday, June 09, 2014

Making Aliases Permanent

Aliases provides you with short cuts to commands or group of commands.

..For example, instead of always having to type ls -la to get the long listing format of a directory's content, you can set up a short form, say ll and type that instead. You do this via aliases by typing in an open terminal this:

alias ll='ls -la'

It would set up an alias of ll that equates to 'ls -la'. As you see the format of setting up an alias is:

alias alias_name="command"

The only issue with this, is that the alias is lost once you end the terminal session. i.e. once you close the terminal. To make the aliase permanent you need to add it in a place where it gets loaded and available at the start of a terminal session.

The most appropiate place to put the aliases in other to make it permanent is in an hidden file called .bashrc in the user's home directory. Or better still, put it in a file usually named as .bash_alaises and have it refereced from .bashrc

This is straight forward.

with vim ~/.bashrc

and you add the aliases in the .bashrc file as shown above. The other, more prefered way is to create another file, usually named .bash_aliases and put your aliases inside instead.

The reason this works is due to the presence of a code section in .bashrc that loads the content of the .bash_alaises.

The portion of the .bashrc looks like this:

If you do not have this piece of code, feel free to add it.

All this has been easy, straight forward and clear enough. The confusing part to all this, especially if you are new to linux, is the seemingly plethora of other files you can put your aliases in and all mysteriously seem to get the job done.

If you do a google search you may find results that points to other files where you can define aliases. /etc/profile, ~/.profile, ~/.bash_login, .bash_profile

The reason these all seems to work is because they are all special kinds of files that have their contents sourced by the shell at different point in time. Some are sourced at login, some are sourced when the shell is invoked interactively.

To really understand how they all fit, lets go over some basic stuff here:

The Shell
I mentioned above that "files are sourced by the shell"... What is the shell? From wikipedia, the definition is rightly put as:

...a user interface for access to an operating system's services. Generally, operating system shells use either a command-line interface (CLI) or graphical user interface (GUI)

We are concerned with the command-line interface. So basically when you fire up a terminal and type a command. Something as simple as "ls". The reason why typing that lists the content of the directory is because the terminal provides you a shell by which you interact with the operating system via commands. There are different kinds of "shells" avaialble especially in the *unix world that provides you with this sort of interface to interacting with the OS. These includes C shell (csh), TC shell (tcsh), Korn shell (ksh), Bourne shell (sh), GNU Bourne-Again Shell (bash) just to name a few.

Login Shell
As it names states. This is the kind of shell that you get when you login in via typing a your username and password. Basically this kind of shell is how you start interacting with your computer. If you use say ssh to login into a remote computer, the session that is started is done in a login shell, and which also happens to be an interactive shell.

Interactive Shell
This is the shell that is created for a session that does not require login. So for instance, after login into say an Ubuntu machine running a GUI, and then you open up a terminal, then you have access to an interactive shell and not a login shell

With these out of the way, we can now see where the seemingly plethoral of shell sourced files fall into.

Executed for login shells and contains contents that takes care of system wide initialization. i.e across all users
Executed by the command interpreter for login shells but only if ~/.bash_profile or ~/.bash_login are absent
Executed by the command interpreter for login shells but only if ~/.bash_profile is absent
Executed for login shells but only for the current logged in user.
The file available per each interactive shell usage.
The individual login shell cleanup file, executed when a login shell exits

So from the listing above it is clear that for start up script the shell looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.

And the reason why something like alias usually goes into ~/.bashrc is because it is only sourced at the point when needed. i.e. when an interactive session is started.

No comments: