Linux Customization With .bashrc

This post will explain what a .bashrc file is and how it can be used to customize your usage in a Linux environment. It will explain what alias and export are used for. Also it will explain how easy it is to create functions. Finally, to tie it all together, it will explain how to set up a colored system-status display that will be shown when you first log into your machine via the command line.

What is a .bashrc file?

A .bashrc file is a startup configuration file used with the Bourne-Again SHell (Bash). Bash is the most common shell on Linux systems, as far as I know at least; I would also assume it is on OS’s like Solaris and Mac OSX too, though I .

What is a .bashrc file used for?

The .bashrc file is used for personal aliases of commands, think shortcuts, and user-functions.

Some quick example .bashrc files

This Novell CoolSolutions page on configuring the .bashrc file is what prompted me to write this entry. This is a fairly lengthy .bashrc file example. You may also just want to google for more examples.

Aliases, Export, Functions, etc.

Alias

You can think of an alias just like you think of a shortcut on your GUI desktop. Effectively they are no different. Isn’t it easier to type something like web instead of cd /opt/lampp/htdocs. If I wanted to, I could have even shortened the alias to just w. Aliases allow you to quickly move around your system as well as execute complex commands with ease.

Export

The export system command can be used in a similar manner as alias. The main difference is that when using export, you create a system variable. An example of this usage is updating the default PATH system variable. For example: export PATH=$PATH:/sbin:/opt/lampp/bin/. This updates the PATH variable to include, what else, other paths. Programs, really the whole system, can access the value of this system variable by referring to $PATH. I mentioned that exporting variables can be used in a similar manner as an alias. An example of this would be creating a system variable called WEB, i.e. export WEB=/opt/lampp/htdocs. On the command line simply execute cd $WEB. This has the exact same effect as having the web alias mentioned above.

Functions

Understanding functions is really quite simple. A function simply wraps around a code block of system commands. An example will probably do the explaining for me:
netinfo () {
echo "---------------- Network Information ----------------"
/sbin/ifconfig | awk /'inet addr/ {print $2}
echo ""
/sbin/ifconfig | awk /'inet addr/ {print $3}
echo ""
/sbin/ifconfig | awk /'inet addr/ {print $4}
echo "------------------------------------------------"
echo ""
}

This block of code, this netinfo function, simply outputs my network information with some nice formatting. From the command line, all I’d have to do is type netinfo.

An example to put it all together

I will put the code here that will allow you to set up your own ‘Welcome / System Status’ display upon logging into your computer via command line. My comments and explanation of what is going on will be shown as comments in the actual code. At the very bottom will be a link to my actual .bashrc file which will include everything I’ve mentioned here.

# WELCOME SCREEN
#######################################################
clear; #remove anything above this welcome screen
for i in `seq 1 15` ; do spin; done ;echo -ne "${WHITE} Welcome to Apache01 ${NC}"; for i in `seq 1 15` ; do spin; done ;echo ""; # this will display a header
echo -e ${LIGHTBLUE}`cat /etc/SuSE-release` ; # this shows some information about my machine itself
echo -e "Kernel Information: " `uname -smr`;
echo "";
echo -ne "Hello $USER today is "; date
echo -e "${WHITE}"; cal ; echo ""; # some date / calendar info
echo -ne "${CYAN}";netinfo; # network info
mountedinfo; echo ""; # filesystem info
raidinfo; echo ""; # RAID info
echo -ne "${LIGHTPURPLE}";fortune; echo ""; # and for a little fun, a fortune
echo -ne "${LIGHTBLUE}Uptime for this computer is ";uptime | awk /'up/ {print $3,$4}' # machine uptime
for i in `seq 1 15` ; do spin; done ;echo -ne "${WHITE} Welcome to Apache01 ${NC}"; for i in `seq 1 15` ; do spin; done ;echo ""; #ending header
echo ""; echo ""

My .bashrc file

A note about the code snippets you see here: I do not claim to be the originator of all this code you see here. Though, much of the code can be found in various places around in the intarweb. Also, if you happen to see anything incorrect, please let me know!

And a question for my those who read this: What interesting things do you have in your .bashrc file?

3 thoughts on “Linux Customization With .bashrc

Leave a Reply

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