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. Each file has a specific use and may affect login and interactive environments differently. The files in the /etc directory generally provide global settings. If an equivalent file exists in your home directory it may override the global settings. An interactive login shell is started after a successful login, using /bin/login, by reading the /etc/passwd file. This shell invocation normally reads /etc/profile and its private equivalent ~/.bash_profile upon startup. An interactive non-login shell is normally started at the command-line (e.g. [prompt]$/bin/bash) or by the /bin/su command. An interactive non-login shell is also started with a terminal program such as xterm or konsole from within a graphical environment. This type of shell invocation normally copies the parent environment and then reads the user's ~/.bashrc file for additional startup configuration instructions. 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 these shell invocations, only the environment inherited from the parent shell is used. The file ~/.bash_logout is not used for an invocation of the shell. It is read and executed when a user exits from an interactive login shell. To the standard files, we also add /etc/bashrc which is called from the user's ~/.bashrc for system wide initialization of non-login shells. For more information see info bash -- Nodes: Bash Startup Files and Interactive Shells. <filename>/etc/profile</filename> Here is a base /etc/profile. This file starts by setting up some helper functions and some basic parameters. It specifies some bash history parameters and, for security purposes, disables keeping a permanent history file for the root user. It also sets a default user prompt. It then calls small, single purpose scripts in the /etc/profile.d directory to provide most initialization. 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. cat > /etc/profile << "EOF" # Begin /etc/profile # Written for Beyond Linux From Scratch # by James Robertson <jameswrobertson@earthlink.net> # modifications by Dagmar d'Surreal <rivyqntzne@pbzpnfg.arg> # 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. # Functions to help us manage paths. Second argument is the name of the # path variable to be modified (default: PATH) pathremove () { local IFS=':' local NEWPATH local DIR local PATHVARIABLE=${2:-PATH} for DIR in ${!PATHVARIABLE} ; do if [ "$DIR" != "$1" ] ; then NEWPATH=${NEWPATH:+$NEWPATH:}$DIR fi done export $PATHVARIABLE="$NEWPATH" } pathprepend () { pathremove $1 $2 local PATHVARIABLE=${2:-PATH} export $PATHVARIABLE="$1${!PATHVARIABLE:+:${!PATHVARIABLE}}" } pathappend () { pathremove $1 $2 local PATHVARIABLE=${2:-PATH} export $PATHVARIABLE="${!PATHVARIABLE:+${!PATHVARIABLE}:}$1" } if [ $EUID -eq 0 ] ; then unset HISTFILE fi # Setup some environment variables. export HISTSIZE=1000 export HISTIGNORE="&:[bf]g:exit" #export PS1="[\u@\h \w]\\$ " export PS1='\u@\h:\w\$ ' for script in /etc/profile.d/*.sh ; do if [ -x $script ] ; then . $script fi done # Now to clean up after ourselves unset pathremove pathprepend pathappend # End /etc/profile EOF Now create the /etc/profile.d directory. install --directory --mode=0755 --owner=root --group=root /etc/profile.d <filename>/etc/profile.d/dircolors.sh</filename> This script uses the ~/.dircolors and /etc/dircolors files to control the colors of file names in a directory listing. They control colorized output of things like ls --color. The explaination of how to initialize these files is at the end of this section. cat > /etc/profile.d/dircolors.sh << "EOF" # 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 alias ls='ls --color=auto' EOF <filename>/etc/profile.d/extrapaths.sh</filename> This script adds several useful paths to the PATH and PKG_CONFIG_PATH environment variables. If you want, you can uncomment the last section to put a dot at the end of your path. This will allow executables in the current working directory to be executed without specifiying a ./, however you are warned that this is generally considered a security hazard. cat > /etc/profile.d/extrapaths.sh << "EOF" if [ -d /usr/local/lib/pkgconfig ] ; then pathappend /usr/local/lib/pkgconfig PKG_CONFIG_PATH fi if [ -d /usr/local/bin ]; then pathprepend /usr/local/bin fi if [ -d /usr/local/sbin -a $EUID -eq 0 ]; then pathprepend /usr/local/sbin fi for directory in $(find /opt/*/lib/pkgconfig -type d); do pathappend $directory PKG_CONFIG_PATH done for directory in $(find /opt/*/bin -type d); do pathappend $directory done if [ -d ~/bin ]; then pathprepend ~/bin fi #if [ $EUID -gt 99 ]; then # pathappend . #fi EOF <filename>/etc/profile.d/readline.sh</filename> This script sets up the default inputrc configuration file. If the user does not have individual settings, it uses the global file. cat > /etc/profile.d/readline.sh << "EOF" # Setup the INPUTRC environment variable. if [ -z "$INPUTRC" -a ! -f "$HOME/.inputrc" ] ; then INPUTRC=/etc/inputrc fi export INPUTRC EOF <filename>/etc/profile.d/tinker-term.sh</filename> Some applications need a specific TERM setting to support color. cat > /etc/profile.d/tinker-term.sh << "EOF" # This will tinker with the value of TERM in order to convince certain apps # that we can, indeed, display color in their window. if [ -n "$COLORTERM" ]; then export TERM=xterm-color fi if [ "$TERM" = "xterm" ]; then export TERM=xterm-color fi EOF <filename>/etc/profile.d/umask.sh</filename> Setting the umask value is important for security. Here we turn off the default group write permissions for system users and when the user name and group name are not the same. cat > /etc/profile.d/umask.sh << "EOF" # By default we want the umask to get set. if [ "$(id -gn)" = "$(id -un)" -a $EUID -gt 99 ] ; then umask 002 else umask 022 fi EOF <filename>/etc/profile.d/X.sh</filename> If X is installed, we also update the PATH and PKG_CONFIG_PATH variables. cat > /etc/profile.d/X.sh << "EOF" if [ -x /usr/X11R6/bin/X ]; then pathappend /usr/X11R6/bin fi if [ -d /usr/X11R6/lib/pkgconfig ] ; then pathappend /usr/X11R6/lib/pkgconfig PKG_CONFIG_PATH fi EOF <filename>/etc/profile.d/xterm-titlebars.sh</filename> This script shows an example of a different way of setting the prompt. The normal variable, PS1, is supplemented by PROMPT_COMMAND. If set, the value of PROMPT_COMMAND is executed as a command prior to issuing each primary prompt. cat > /etc/profile.d/xterm-titlebars.sh << "EOF" # The substring match ensures this will work for "xterm" and "xterm-xfree86". if [ "${TERM:0:5}" = "xterm" ]; then PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME} : ${PWD}\007"' export PROMPT_COMMAND fi EOF Other initialization can easily be added to the profile by adding additional scripts to the /etc/profile.d directory. <filename>/etc/bashrc</filename> Here is a base /etc/bashrc. Comments in the file should explain everything you need. cat > /etc/bashrc << "EOF" # Begin /etc/bashrc # Written for Beyond Linux From Scratch # by James Robertson <jameswrobertson@earthlink.net> # 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 # Provides a colored /bin/ls command. Used in conjunction with code in # /etc/profile. alias ls='ls --color=auto' # Provides prompt for non-login shells, specifically shells started # in the X environment. [Review the LFS archive thread titled # PS1 Environment Variable for a great case study behind this script addendum.] #export PS1="[\u@\h \w]\\$ " export PS1='\u@\h:\w\$ ' # End /etc/bashrc EOF <filename>~/.bash_profile</filename> Here is a base ~/.bash_profile. If you want each new user to have this file automatically, just change the output of the command to /etc/skel/.bash_profile and check the permissions after the command is run. You can then copy /etc/skel/.bash_profile to the home directories of already existing users, including root, and set the owner and group appropriately. cat > ~/.bash_profile << "EOF" # Begin ~/.bash_profile # Written for Beyond Linux From Scratch # by James Robertson <jameswrobertson@earthlink.net> # updated by Bruce Dubbs <bdubbs@linuxfromscratch.org> # 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. append () { # First remove the directory local IFS=':' local NEWPATH for DIR in $PATH; do if [ "$DIR" != "$1" ]; then NEWPATH = ${NEWPATH:+$NEWPATH:}$DIR fi done # Then append the directory export PATH=$NEWPATH:$1 } if [ -f "$HOME/.bashrc" ] ; then source $HOME/.bashrc fi if [ -d "$HOME/bin" ] ; then append $HOME/bin fi unset append # End ~/.bash_profile EOF <filename>~/.bashrc</filename> Here is a base ~/.bashrc. The comments and instructions for using /etc/skel for .bash_profile above also apply here. Only the target file names are different. cat > ~/.bashrc << "EOF" # Begin ~/.bashrc # Written for Beyond Linux From Scratch # by James Robertson <jameswrobertson@earthlink.net> # 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 EOF <filename>~/.bash_logout</filename> This is an empty ~/.bash_logout that can be used as a template. 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. cat > ~/.bash_logout << "EOF" # Begin ~/.bash_logout # Written for Beyond Linux From Scratch # by James Robertson <jameswrobertson@earthlink.net> # Personal items to perform on logout. # End ~/.bash_logout EOF <filename>/etc/dircolors</filename> If you want to use the dircolors capability, then run the following command. The /etc/skel setup steps seen above also can be used here to provide a .dircolors file when a new user is set up. As before, just change the output file name on the following command and assure the owner and group permissions are correct on the files created and/or copied. dircolors -p > /etc/dircolors If you wish to customize the colors used for different file types, you can edit the /etc/dircolors file. The instructions for setting the colors are embedded in the file. Finally, 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.