These classnotes are depreciated. As of 2005, I no longer teach the classes. Notes will remain online for legacy purposes

UNIX01/BASH Configuration

Classnotes | UNIX01 | RecentChanges | Preferences

Since BASH is already a capable interpretive language, the configuration files for it are written as shell scripts. Whenever you login or login, BASH executes a set of known shell scripts to define configurations and environments. You can tweak your configuration and environment by modifying these scripts.

The BASH Shell Login Initialization File: .bash_profile

The .bash_profile file is the BASH shell's login initialization file. It is a script file that is automatically executed whenever a user logs in. The file contains shell commands that define special environment variables used to manage your shell. They may be either redefinitions of system-defined special variables or definitions of user-defined special variables. For example, when you log in, your user shell needs to know what directories hold Linux commands. It will reference the PATH variable in order to find the path names for these directories. However, the PATH variable must first be assigned those names. In the .bash_profile file tehre is usually an assignment operation that does just that. Often times, it is an appendment to the system-defined PATH variable:
 PATH="$PATH:/home/sam/bin"
 export PATH

Note the use of the export command. This ensures that subsequent shells have access to the variables defined in .bash_profile.

/etc/profile

Chances are, your Linux installation will also have a system-wide profile script located in /etc/profile. This script usually runs before .bash_profile (sometimes, it is the first command to be run in .bash_profile) and sets system-wide settings for every user. For example, on my Debian system, my /etc/profile file looks like this

 umask 022
 PATH="/sbin:/bin:/usr/sbin:/usr/bin:
     /usr/X11R6/bin:/usr/local/sbin:/usr/local/bin:/usr/games"
 export PATH
 # Set LOCALE
 if [ -f /etc/sysconfig/i18n ]; then
    . /etc/sysconfig/i18n
    [ -n "$LANG" ] || LANG="de_DE"
    export LANG
    other misc things
 fi
 # END LOCALE
 # enable bash completion in interactive shells
 [ "$PS1" -a -f /etc/bash_completion ] && . /etc/bash_completion
 PS1="\u@\h:\w\\$ "
 alias which="type -path"
 alias where="type -all"
 alias ll="ls -l --color=auto"
 alias rm="rm -i"
 alias mv="mv -i"
 alias cp="cp -i"
 alias la="ls -la --color=auto"
 alias ls="ls --color=auto"

The BASH Shell Initialization File: .bashrc

The .bashrc file is an initialization file that is executed each time you enter the BASH shell or generate any sub-shells. In other words, it is run each time you start a new XTerm. You can think of the .bash_profile script as running once per session, but the .bashrc file runs every time to start a new terminal inside that session.

The BASH Shell Lohout Initialization FIle: .bash_logout

The .bash_logout file is also an initialization file, which is executed when the user logs out. It is designed to perform any operations you want doen whenever you log out. Instead of variable definitions, the .bash_logout script usually contains shell commands that form a kind of shutdown procedure-- actions you always want taken before you log out. One common logout command is to clear the screen and then issue a farewell.

On most Linux systems, the .bash_logout file is not created when your account was added. This is generally due to the fact that it's use has depreciated somewhat over the years. When you are logged into a graphical display, your .bash_logout file is generally meaningless as nothing it displays will be written to your screen. However, you may find you have specific need (such as disconnecting network drives) where .bash_logout can come in handy.

Here is a simple example .bash_logout script

 clear
 echo "Goodbye for now"


Classnotes | UNIX01 | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited July 26, 2003 1:11 pm (diff)
Search:
(C) Copyright 2003 Samuel Hart
Creative Commons License
This work is licensed under a Creative Commons License.