In “What Is the Shell?,” you learned about the login sequence. This sequence is completed when your shell displays your command prompt and waits for you to type your first command. Just before it does that, however, your login shell executes two special files on the system. The first is /etc/profile. This file is set up by the system administrator and usually does things like checking to see whether you have mail (Where do you think the “You have mail.” message comes from?), setting your default file creation mask (your umask), assigning values to some standard exported variables, and anything else that the administrator wants to have executed whenever a user logs in.
The second file that gets automatically executed is .profile in your home directory. Your system administrator may have given you a default .profile file when you got your account. See what’s in it now:
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">$ cat $HOME/.profile</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">PATH="/bin:/usr/bin:/usr/lbin:.:"</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">export PATH</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">$</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
Here you see a small .profile file that simply sets the PATH and exports it.
You can change your .profile file to include any commands that you want executed whenever you log in. You can even put commands in your .profile file that override settings (usually environment variables) made in /etc/profile. Note that the commands in /etc/profile and .profile are executed by your login shell (as if you typed in
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">$ . /etc/profile</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">$ . .profile</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">$</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
as soon as you logged in), which means that changes made to your environment remain after the programs are executed.
Here’s a sample .profile that sets your PATH to include your own bin, sets your CDPATH, changes your primary and secondary command prompts, changes your erase character to a backspace (Ctrl+h) with the stty command, and prints a friendly message using the greetings program “Decisions, Decisions”:
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">$ cat $HOME/.profile</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">PATH=/bin:/usr/bin:/usr/lbin:$HOME/bin:.:</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">CDPATH=.:$HOME:$HOME/misc:$HOME/documents</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">PS1="=> "</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">PS2="====> "</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">export PATH CDPATH PS1 PS2</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">stty echoe erase CTRL+h</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">echo</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">greetings</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">$</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
Here’s what a login sequence would look like with this .profile:
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">login: steve</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">Password:</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">Good morning Output from greetings</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">=> New PS1</span>