The Bash Shell Startup Files

The shell program /bin/bash (hereafter referred to as just "the shell") uses a collection of startup files to help create an environment to run in. Each file has a specific use and may affect login and interactive environments differently.

An interactive login shell is started after a successful login by /bin/login by reading the /etc/passwd file. An interactive non-login shell is started at the command line (e.g. [prompt]$/bin/bash). A non-interactive shell is usually present when a shell script is running. It is non-interactive because it is processing a script and not waiting for user input between commands.

For more information see info bash -- Nodes: Bash Startup Files and Interactive Shells

The following files are needed to make sure that the correct environment is read for each of the ways the shell can be invoked: /etc/profile, /etc/bashrc, ~/.bash_profile, and ~/.bashrc. The file ~/.bash_logout is not used for an invocation of the shell. It is read by the shell when a user logouts of the system. The files /etc/profile and ~/.bash_profile are read when the shell is invoked as a interactive login shell. The file ~/.bashrc is read when the shell is invoked as an interactive non-login shell.

Here is a base /etc/profile. Comments in the file should explain everything you need. For more information on the escape sequences you can use for your prompt (e.g. the PS1 environment variable) see info bash -- Node: Printing a Prompt.

# Begin /etc/profile
# Written for Beyond Linux From Scratch
# by James Robertson <[email protected]>

# System wide environment variables and startup programs.

# System wide aliases and functions should go in /etc/bashrc.  Personal
# environment variables and startup programs should go into
# ~/.bash_profile.  Personal aliases and functions should go into
# ~/.bashrc.

# Function to help us manage paths
pathman () {
	if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
		if [ "$2" = "last" ] ; then
			PATH=$PATH:$1
		else
			PATH=$1:$PATH
		fi
	fi
}

# Add to the standard path.
if [ $(id -u) = 0 ] ; then
	if [ -d "/usr/local/sbin" ] ; then
		pathman /usr/local/sbin last
	fi
fi

if [ $(id -u) != 0 ] ; then
	if [ -d "/usr/local/bin" ] ; then
		pathman /usr/local/bin last
	fi
fi

if [ -d "/usr/X11R6/bin" ] ; then
	pathman /usr/X11R6/bin last
fi

# Setup some environment variables.
HISTSIZE=1000
PS1="[\u@\h \w]\\$ "

# Setup the INPUTRC environment variable.
if [ -z "$INPUTRC" -a ! -f "$HOME/.inputrc" ] ; then
	INPUTRC=/etc/inputrc
fi

# Setup for /bin/ls to support color, the alias is in /etc/bashrc.
if [ -f "/etc/dircolors" ] ; then
	eval $(dircolors -b /etc/dircolors)

	if [ -f "$HOME/.dircolors" ] ; then
		eval $(dircolors -b $HOME/.dircolors)
	fi
fi

export PATH HISTSIZE PS1 LS_COLORS INPUTRC

# End /etc/profile

Here is a base /etc/bashrc. Comments in the file should explain everything you need.

# Begin /etc/bashrc 
# Written for Beyond Linux From Scratch 
# by James Robertson <[email protected]>

# System wide aliases and functions.

# System wide environment variables and startup programs should go into
# /etc/profile.  Personal environment variables and startup programs
# should go into ~/.bash_profile.  Personal aliases and functions should
# go into ~/.bashrc

# By default we want the umask to get set.
# Even for non-interactive and non-login shells.
if [ "$(id -gn)" = "$(id -un)" -a $(id -u) -gt 99 ] ; then
	umask 002
else
	umask 022
fi

# Provides a colored /bin/ls command.  Used in conjunction with code in
# /etc/profile.
alias ls='ls --color=auto'

# End /etc/bashrc

Here is a base ~/.bash_profile. Comments in the file should explain everything you need.

# Begin ~/.bash_profile
# Written for Beyond Linux From Scratch
# by James Robertson <[email protected]>

# Personal environment variables and startup programs.

# Personal aliases and functions should go in ~/.bashrc.  System wide
# environment variables and startup programs are in /etc/profile.
# System wide aliases and functions are in /etc/bashrc.

if [ -f "$HOME/.bashrc" ] ; then
	source $HOME/.bashrc
fi

if [ -d "$HOME/bin" ] ; then
	pathman $HOME/bin last
fi

export PATH 

# End ~/.bash_profile

Here is a base ~/.bashrc. Comments in the file should explain everything you need.

# Begin ~/.bashrc
# Written for Beyond Linux From Scratch
# by James Robertson <[email protected]>

# Personal aliases and functions.

# Personal environment variables and startup programs should go in
# ~/.bash_profile.  System wide environment variables and startup
# programs are in /etc/profile.  System wide aliases and functions are
# in /etc/bashrc. 

if [ -f "/etc/bashrc" ] ; then
	source /etc/bashrc
fi

# End ~/.bashrc

Here is a base ~/.bash_logout. Comments in the file should explain everything you need. You will notice that the base ~/.bash_logout does not include a clear command. This is because the clear is handled in the /etc/issue file.

# Begin ~/.bash_logout
# Written for Beyond Linux From Scratch
# by James Robertson <[email protected]>

# Personal items to perform on logout.

# End ~/.bash_logout

If you want to use the /etc/dircolors or ~/.dircolors files called from /etc/profile, then run the following: /bin/dircolors -p > /etc/dircolors or /bin/dircolors -p > ~/.dircolors respectively. The file in the /etc directory should be used for global settings and if one exists in your home directory then it will overwrite the global settings. It might be a good idea to create a base .dircolors file and place it in the /etc/skel directory for new users.

Ian Macdonald has written an excellent collection of tips and tricks to enhance your shell environment. You can read it online at http://www.caliban.org/bash/index.shtml