Beyond Linux From Scratch - Version 6.1

Chapter 3. After LFS Configuration Issues

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 using a shell program (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.

Many distributions use /etc/bashrc for
system wide initialization of non-login shells. This file is usually
called from the user's ~/.bashrc file and
is not built directly into bash itself. This convention is followed in
this section.

Note

Most of the instructions below are used to create files located
in the /etc directory structure which
requires you to execute the commands as the root user. If you elect to create the files in
user's home directories instead, you should run the commands as
an unprivileged user.

/etc/profile

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
of the initialization.

For more information on the escape sequences you can use for your
prompt (i.e., the PS1 environment variable)
see info bash --
Node: Printing a
Prompt.

The
/etc/profile.d Directory

/etc/profile.d/dircolors.sh

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 explanation of how to initialize
these files is at the end of this section.

/etc/profile.d/extrapaths.sh

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.

/etc/profile.d/tinker-term.sh

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

/etc/profile.d/umask.sh

Setting the umask
value is important for security. Here the default group write
permissions are turned off for system users and when the user
name and group name are not the same.

/etc/profile.d/extra-prompt.sh

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. The sequence \e
is an ESC character. \a is a BEL character. For a reference on
xterm escape
sequences, see http://rtfm.etla.org/xterm/ctlseq.html.

'/etc/profile.d/i18n.sh'

This script shows how to set some environment variables necessary
for native language support. Setting these variables properly
gives you:

the output of programs translated into your native language

correct classification of characters into letters, digits
and other classes – this is necessary for
Bash to accept keystrokes
properly in non-English locales

the alphabetical sorting order correct for your country

proper default paper size

correct formatting of monetary, time and date values

Replace [ll] with the
two-letter code for your language (e.g., “en”) and [CC] with the two-letter code for
your country (e.g., “GB”).
Also you may need to specify (and this is actually the preferred
form) your character encoding (e.g., “iso8859-1”) after a dot (so that the result
is “en_GB.iso8859-1”).
Issue the following command for more information:

man 3 setlocale

The list of all locales supported by Glibc can be obtained by running the
following command:

locale -a

After you are sure about your locale settings, create the
/etc/profile.d/i18n.sh file:

~/.bash_profile

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.

~/.bash_logout

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.

/etc/dircolors

If you want to use the dircolors
capability, then run the following command. The /etc/skel setup steps shown 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 permissions, owner, and
group 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.