mirror of
https://github.com/Zeckmathederg/glfs.git
synced 2025-01-27 18:02:12 +08:00
5bc79f991e
git-svn-id: svn://svn.linuxfromscratch.org/BLFS/trunk/BOOK@1693 af4574ff-66df-0310-9fd7-8a98e5e911e0
254 lines
8.7 KiB
XML
254 lines
8.7 KiB
XML
<sect1 id="postlfs-config-profile" xreflabel="The Bash Shell Startup Files">
|
|
<?dbhtml filename="profile.html" dir="postlfs"?>
|
|
<title>The Bash Shell Startup Files</title>
|
|
|
|
<para>The shell program <filename>/bin/bash</filename> (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. The files in
|
|
the <filename class="directory">/etc</filename> directory generally provide global
|
|
settings. If an equivalent file exists in your home directory it may
|
|
override the global settings.
|
|
</para>
|
|
|
|
<para>An interactive login shell is started after a successful login, using
|
|
<filename>/bin/login</filename>, by reading the
|
|
<filename>/etc/passwd</filename> file. An
|
|
interactive non-login shell is started at the command-line (e.g.
|
|
<prompt>[prompt]$</prompt><command>/bin/bash</command>). 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.</para>
|
|
|
|
<para>For more information see <command>info bash</command> --
|
|
<emphasis role="strong">Nodes: Bash Startup Files and Interactive
|
|
Shells.</emphasis></para>
|
|
|
|
<para>The following files are used to make sure that the correct
|
|
environment is established for each of the ways the shell can be invoked:
|
|
<filename>/etc/profile</filename> and its private equivalent
|
|
<filename>~/.bash_profile</filename>, and
|
|
<filename>/etc/bashrc</filename> (unofficial) and its private equivalent
|
|
<filename>~/.bashrc</filename>.
|
|
</para>
|
|
|
|
<para>
|
|
The file <filename>~/.bash_logout</filename> is not used for an
|
|
invocation of the shell. It is read by the shell when a user logs out
|
|
of the system.</para>
|
|
|
|
<para>The files <filename>/etc/profile</filename> and
|
|
<filename>~/.bash_profile</filename> are read when the shell is invoked
|
|
as an interactive login shell. The file <filename>~/.bashrc</filename>
|
|
is read when the shell is invoked as an interactive non-login
|
|
shell and it reads <filename>/etc/bashrc</filename> if it exists</para>
|
|
|
|
<para>Also useful are the <filename>/etc/dircolors</filename> and
|
|
<filename>~/.dircolors</filename> files called from
|
|
<filename>/etc/profile</filename>. They control colorized output of
|
|
things like <command>ls --color</command>.
|
|
</para>
|
|
|
|
<para>Here is a base <filename>/etc/profile</filename>. 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
|
|
<envar>PS1</envar> environment variable) see <command>info
|
|
bash</command> -- <emphasis role="strong">Node: Printing a
|
|
Prompt.</emphasis></para>
|
|
|
|
<screen><userinput><command>cat > /etc/profile << "EOF"</command>
|
|
# Begin /etc/profile
|
|
# Written for Beyond Linux From Scratch
|
|
# by James Robertson <jameswrobertson@earthlink.net>
|
|
|
|
# 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
|
|
HISTIGNORE="&:[bf]g:exit"
|
|
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 HISTIGNORE PS1 LS_COLORS INPUTRC
|
|
|
|
# End /etc/profile
|
|
<command>EOF</command></userinput></screen>
|
|
|
|
<para>Here is a base <filename>/etc/bashrc</filename>. Comments in the
|
|
file should explain everything you need.</para>
|
|
|
|
<screen><userinput><command>cat > /etc/bashrc << "EOF"</command>
|
|
# 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
|
|
|
|
# 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'
|
|
|
|
# Provides prompt for non-interactive shells, specifically shells started
|
|
# in the xfree environment. [Review archive thread titled PS1
|
|
# Environment Variable for a great case study behind this script
|
|
# addendum.]
|
|
export PS1="[\u@\h \w]\\$ "
|
|
|
|
# End /etc/bashrc
|
|
<command>EOF</command></userinput></screen>
|
|
|
|
<para>Here is a base <filename>~/.bash_profile</filename>. Comments in
|
|
the file should explain everything you need. If you want each new user
|
|
to have this file automatically provided, just change the output of the
|
|
next command to <filename>/etc/skel/.bash_profile</filename> and check the
|
|
permissions after the command is run. You can then copy
|
|
<filename>/etc/skel/.bash_profile</filename> to the home directories of
|
|
already existing users, including root, and set the owner and group
|
|
appropriately.
|
|
</para>
|
|
|
|
<screen><userinput><command>cat > ~/.bash_profile << "EOF"</command>
|
|
# Begin ~/.bash_profile
|
|
# Written for Beyond Linux From Scratch
|
|
# by James Robertson <jameswrobertson@earthlink.net>
|
|
|
|
# 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
|
|
<command>EOF</command></userinput></screen>
|
|
|
|
<para>Here is a base <filename>~/.bashrc</filename>. Comments in the
|
|
file should explain everything you need. The comments and
|
|
instructions for using <filename class="directory">/etc/skel</filename> for
|
|
<filename>.bash_profile</filename> above also apply here. Only the
|
|
target file names are different.</para>
|
|
|
|
<screen><userinput><command>cat > ~/.bashrc << "EOF"</command>
|
|
# 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
|
|
<command>EOF</command></userinput></screen>
|
|
|
|
<para>Here is a base <filename>~/.bash_logout</filename>. Comments in
|
|
the file should explain everything you need. You will notice that the
|
|
base <filename>~/.bash_logout</filename> does not include a
|
|
<userinput>clear</userinput> command. This is because the clear is
|
|
handled in the <filename>/etc/issue</filename> file.</para>
|
|
|
|
<screen><userinput><command>cat > ~/.bash_logout << "EOF"</command>
|
|
# Begin ~/.bash_logout
|
|
# Written for Beyond Linux From Scratch
|
|
# by James Robertson <jameswrobertson@earthlink.net>
|
|
|
|
# Personal items to perform on logout.
|
|
|
|
# End ~/.bash_logout
|
|
<command>EOF</command></userinput></screen>
|
|
|
|
<para>
|
|
If you want to use the <filename>dircolors</filename> capability
|
|
then run the following command. The <filename class="directory">/etc/skel</filename> setup
|
|
steps seen above also can be used here to provide a
|
|
<filename>.dircolors</filename> 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.
|
|
</para>
|
|
|
|
<para>
|
|
<userinput><command>dircolors -p > /etc/dircolors</command></userinput>
|
|
</para>
|
|
|
|
<para>Ian Macdonald has written an excellent collection of tips and
|
|
tricks to enhance your shell environment. You can read it online at
|
|
<ulink
|
|
url="http://www.caliban.org/bash/index.shtml">http://www.caliban.org/bash/index.shtml</ulink></para>
|
|
|
|
</sect1>
|