BASH is one of the most popular shells you can find in GNU/LiNUX, it has a set of configuration files that you must understand in order to customized it and be more productive! So what file should I use? when is this file executed?, what is a login shell?,… those are questions new users are going to have about Bash configuration files so in this article I will show a simple way to understand how they work.
Please consider that I’m going to use Debian GNU/LiNUX 5.0 out-of-the-box so if you’re using another distribution or unix remember that file names could change. Let’s start!
Those files are system wide configurations, that means all users are going to use them. If you need to customize that startup files only for your personal user, you need to modify these files:
Well, the easiest way to figure out how Bash works with this files is adding logs in all of them. So execute the following lines with root user:
echo "echo 'running /etc/bash.bashrc'" >> /etc/bash.bashrc echo "echo 'running /etc/profile'" >> /etc/profile
And these lines with your personal user (non-root):
echo "echo 'running ~/.bashrc'" >> ~/.bashrc echo "echo 'running ~/.profile'" >> ~/.profile echo "echo 'running ~/.bash_logout'" >> ~/.bash_logout
Everything is done so let’s prove it starting an ssh session (you can also open a session in a text virtual terminal):
modlost@optimus:~$ ssh firstname.lastname@example.org email@example.com\'s password: ******** Linux ironhide 2.6.26-2-686 #1 SMP Thu Sep 16 19:35:51 UTC 2010 i686 The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Last login: Mon Oct 15 12:53:39 2010 from 192.168.0.2 running /etc/profile running ~/.bashrc running ~/.profile modlost@ironhide:~$ logout running ~/.bash_logout
This is a login shell (it’s pretty clear, isn’t it?) and as you can see files /etc/profile, ~/.profile and ~/.bashrc were executed when you login and when you logout only file ~/.bash_logout is called. Do not forget that if you login you should logout. Well, that’s a login shell so how can you start a non-login shell? The easiest way is using the command ‘su’, let’s see what happen:
modlost@ironhide:~$ su modlost Password: ********nrunning /etc/bash.bashrc running ~/.bashrc modlost@ironhide:~$ exit
Files /etc/bash.bashrc and ~/.bashrc were executed and remember you didn’t login so trying to logout is not allowed, you should exit from that shell and when you exit none file is called. What do you do if you need ‘su’ command to start a login shell? Use dash parameter:
modlost@ironhide:~$ su - modlost Password: running /etc/profile running ~/.bashrc running ~/.profile modlost@ironhide:~$ logout running ~/.bash_logout
The result is the same we had with ssh session (login shell). Let’s move to graphic environments! What do you think could happen in X Window? Launch a Terminal from you preferred desktop environment:
running /etc/bash.bashrc running ~/.bashrcnmodlost@ironhide:~$
It’s a non-login shell, isn’t it?. Yeah! Can you see the pattern?
- Login shell:
- ~/.profile (in its code, it executes ~/.bashrc)
- Non-login shell:
No matter how you start a session (ssh, text or graphic) a login shell is created, that’s why it’s common to see in /etc/profile and ~/.profile configurations about user file-creation mask or variables like $PATH and $PS1 so other shells/applications can have them already defined.
After you login, you can open any interactive non-login shells using ‘su’ command or terminal applications so in files /etc/bash.bashrc and ~/.bashrc you will see configurations about colors, history, etc. to do your interactive shells human-friendly.
Another thing you can find is that file ~/.bashrc is called in login and non-login shells so you can use ~/.bashrc to store configurations you want to apply in both kind of shells each time they are created, for that reason it’s common to set aliases in this file.
Finally, I recommend you to try other ways to start shells so you can see what files are called and understand interactive, non-interactive, login and non-login shells!