Showing posts with label unix. Show all posts
Showing posts with label unix. Show all posts

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: