The .bash_profile, .bash_logout, and .bashrc Files

3.1 The .bash_profile, .bash_logout, and .bashrc Files

Three files in your home directory have a special meaning to bash , providing a way for you to set up your account environment automatically when you log in and when you invoke another bash shell, and allowing you to perform commands when you log out. These files may already exist in your home directory, depending on how your system administrator has set up your account. If they don't exist, your account is using only the default system file /etc/profile . You can easily create your own bash files using your favorite text editor. If you are unfamiliar with text editors available under UNIX, we suggest that you familiarize yourself with one of the better-known ones such as vi or emacs before proceeding further with the techniques described in this chapter.

The most important bash file, .bash_profile , is read and the commands in it executed by bash every time you log in to the system. If you examine your .bash_profile you will probably see lines similar to:

 PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin 
 SHELL=/bin/bash 
 MANPATH=/usr/man:/usr/X11/man 
 EDITOR=/usr/bin/vi 
 
  PS1='\h:\w$ '  
  PS2='> '  
  export EDITOR  

These lines define the basic environment for your login account. For the moment, it is probably best to leave these lines alone until you understand what they do. When editing your .bash_profile , just add your new lines after the existing ones.

Note that whatever you add to your .bash_profile won't take effect until the file is re-read by logging out and then logging in again. Alternatively, you can also use the source command. [1] For example:

[1] You can also use the synonymous command dot (.).

  source .bash_profile  

source executes the commands in the specified file, in this case .bash_profile , including any commands that you have added.

bash allows two synonyms for .bash_profile : .bash_login , derived from the C shell's file named .login , and .profile , derived from the Bourne shell and Korn shell files named .profile . Only one of these three is read when you log in. If .bash_profile doesn't exist in your home directory, then bash will look for .bash_login . If that doesn't exist it will look for .profile .

One advantage of bash 's ability to look for either synonym is that you can retain your .profile if you have been using the Bourne shell. If you need to add bash -specific commands, you can put them in .bash_profile followed by the command source .profile . When you log in, all the bash -specific commands will be executed, and bash will source .profile , executing the remaining commands. If you decide to switch to using the Bourne shell you don't have to modify your existing files. A similar approach was intended for .bash_login and the C shell .login , but due to differences in the basic syntax of the shells , this is not a good idea.

.bash_profile is read and executed only by the login shell. If you start up a new shell (a subshell ) by typing bash on the command line, it will attempt to read commands from the file .bashrc . This scheme allows you the flexibility to separate startup commands needed at login time from those you might need when you run a subshell. If you need to have the same commands run regardless of whether it is a login shell or a subshell, you can just use the source command from within .bash_profile to execute .bashrc . If .bashrc doesn't exist then no commands are executed when you start up a subshell.

The file .bash_logout is read and executed every time a login shell exits. It is provided to round out the capabilities for customizing your environment. If you wanted to execute some commands that remove temporary files from your account or record how much time you have spent logged in to the system then you would place the commands in .bash_logout . This file doesn't have to exist in your account ”if it isn't there when you log out, then no extra commands are executed.

 



Learning the Bash Shell
Learning the bash Shell: Unix Shell Programming (In a Nutshell (OReilly))
ISBN: 0596009658
EAN: 2147483647
Year: 1998
Pages: 104

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net