Organizing “.bashrc”

17 / Aug / 2011 by Himanshu Seth 3 comments

I have been working on Linux for almost 3 years now and have grown to love it a lot.

As with every linux user, the .bashrc file becomes cumbersome and difficult to maintain over a period of time, since there are a lot of project specific aliases, paths, etc

So after working on a couple of projects, I started maintaining separate files for my environment settings. So I just had to create a separate project setting file:

[bash]~/.myProjectSettings[/bash]
[bash]
alias myAlias1=’cd /to/project/dir’
alias projectqa=’ssh to@qa’
alias projectDb=’mysql -u<username> -p<password> <project_db_name>’
[/bash]
So, everytime I had to add more project specific settings or separate my settings, I had to create a new settings file and then source it in my .bashrc by adding this line

[bash]. ~/.myProjectSettings[/bash]

As you can guess, after doing this a couple of times, even this was becoming very tedious.

So, I tried to follow the golden rule of convention over configuration.

In my .bashrc, I added the following function :
[bash]
function sourceAllMySettings(){
FOLDER_TO_BE_SOURCED=~/.mySettings/
for i in `find $FOLDER_TO_BE_SOURCED -type f`;
do
. $i
done;
}
##don’t forget to execute the function
sourceAllMySettings
[/bash]
and placed all my setting files into the folder :
[bash]~/.mySettings/[/bash]

So, now if I have to add more settings to my .bashrc, I just create a new executable file in this folder and it gets automatically sourced.

Goodbye messy .bashrc, welcome organized (segregated) settings 🙂

Regards
~~Himanshu Seth~~
http://www.tothenew.com

FOUND THIS USEFUL? SHARE IT

Tag -

Linux Ubuntu

comments (3)

  1. Hitesh Bhatia

    Hi Himanshu , this one really helped me to clean my .bash_profile , Thanks .

    Just one thing ,
    8|don’t forget to execute the function
    9|sourceAllMySettigns
    you’ve misspelled name of function here .

    Reply

Leave a Reply to Hitesh Bhatia Cancel reply

Your email address will not be published. Required fields are marked *