old-www/LDP/Bash-Beginners-Guide/html/sect_03_01.html

798 lines
16 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML
><HEAD
><TITLE
>Shell initialization files</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Bash Guide for Beginners"
HREF="index.html"><LINK
REL="UP"
TITLE="The Bash environment"
HREF="chap_03.html"><LINK
REL="PREVIOUS"
TITLE="The Bash environment"
HREF="chap_03.html"><LINK
REL="NEXT"
TITLE="Variables"
HREF="sect_03_02.html"></HEAD
><BODY
CLASS="sect1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Bash Guide for Beginners</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="chap_03.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 3. The Bash environment</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="sect_03_02.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="sect_03_01"
></A
>3.1. Shell initialization files</H1
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="sect_03_01_01"
></A
>3.1.1. System-wide configuration files</H2
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="sect_03_01_01_01"
></A
>3.1.1.1. /etc/profile</H3
><P
>When invoked interactively with the <TT
CLASS="option"
>--login</TT
> option or when invoked as <B
CLASS="command"
>sh</B
>, Bash reads the <TT
CLASS="filename"
>/etc/profile</TT
> instructions. These usually set the shell variables <TT
CLASS="varname"
>PATH</TT
>, <TT
CLASS="varname"
>USER</TT
>, <TT
CLASS="varname"
>MAIL</TT
>, <TT
CLASS="varname"
>HOSTNAME</TT
> and <TT
CLASS="varname"
>HISTSIZE</TT
>.</P
><P
>On some systems, the <B
CLASS="command"
>umask</B
> value is configured in <TT
CLASS="filename"
>/etc/profile</TT
>; on other systems this file holds pointers to other configuration files such as:</P
><P
></P
><UL
><LI
><P
><TT
CLASS="filename"
>/etc/inputrc</TT
>, the system-wide Readline initialization file where you can configure the command line bell-style.</P
></LI
><LI
><P
>the <TT
CLASS="filename"
>/etc/profile.d</TT
> directory, which contains files configuring system-wide behavior of specific programs.</P
></LI
></UL
><P
>All settings that you want to apply to all your users' environments should be in this file. It might look like this:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="screen"
>&#13;# /etc/profile
# System wide environment and startup programs, for login setup
PATH=$PATH:/usr/X11R6/bin
# No core files by default
ulimit -S -c 0 &#62; /dev/null 2&#62;&#38;1
USER="`id -un`"
LOGNAME=$USER
MAIL="/var/spool/mail/$USER"
HOSTNAME=`/bin/hostname`
HISTSIZE=1000
# Keyboard, bell, display style: the readline config file:
if [ -z "$INPUTRC" -a ! -f "$HOME/.inputrc" ]; then
INPUTRC=/etc/inputrc
fi
PS1="\u@\h \W"
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC PS1
# Source initialization files for specific programs (ls, vim, less, ...)
for i in /etc/profile.d/*.sh ; do
if [ -r "$i" ]; then
. $i
fi
done
# Settings for program initialization
source /etc/java.conf
export NPX_PLUGIN_PATH="$JRE_HOME/plugin/ns4plugin/:/usr/lib/netscape/plugins"
PAGER="/usr/bin/less"
unset i
</PRE
></FONT
></TD
></TR
></TABLE
><P
>This configuration file sets some basic shell environment variables as well as some variables required by users running Java and/or Java applications in their web browser. See <A
HREF="sect_03_02.html"
>Section 3.2</A
>.</P
><P
>See <A
HREF="chap_07.html"
>Chapter 7</A
> for more on the conditional <B
CLASS="command"
>if</B
> used in this file; <A
HREF="chap_09.html"
>Chapter 9</A
> discusses loops such as the <B
CLASS="command"
>for</B
> construct.</P
><P
>The Bash source contains sample <TT
CLASS="filename"
>profile</TT
> files for general or individual use. These and the one in the example above need changes in order for them to work in your environment!</P
></DIV
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="sect_03_01_01_02"
></A
>3.1.1.2. /etc/bashrc</H3
><P
>On systems offering multiple types of shells, it might be better to put Bash-specific configurations in this file, since <TT
CLASS="filename"
>/etc/profile</TT
> is also read by other shells, such as the Bourne shell. Errors generated by shells that don't understand the Bash syntax are prevented by splitting the configuration files for the different types of shells. In such cases, the user's <TT
CLASS="filename"
>~/.bashrc</TT
> might point to <TT
CLASS="filename"
>/etc/bashrc</TT
> in order to include it in the shell initialization process upon login.</P
><P
>You might also find that <TT
CLASS="filename"
>/etc/profile</TT
> on your system only holds shell environment and program startup settings, while <TT
CLASS="filename"
>/etc/bashrc</TT
> contains system-wide definitions for shell functions and aliases. The <TT
CLASS="filename"
>/etc/bashrc</TT
> file might be referred to in <TT
CLASS="filename"
>/etc/profile</TT
> or in individual user shell initialization files.</P
><P
>The source contains sample <TT
CLASS="filename"
>bashrc</TT
> files, or you might find a copy in <TT
CLASS="filename"
>/usr/share/doc/bash-2.05b/startup-files</TT
>. This is part of the <TT
CLASS="filename"
>bashrc</TT
> that comes with the Bash documentation:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="screen"
>&#13;alias ll='ls -l'
alias dir='ls -ba'
alias c='clear'
alias ls='ls --color'
alias mroe='more'
alias pdw='pwd'
alias sl='ls --color'
pskill()
{
local pid
pid=$(ps -ax | grep $1 | grep -v grep | gawk '{ print $1 }')
echo -n "killing $1 (process $pid)..."
kill -9 $pid
echo "slaughtered."
}
</PRE
></FONT
></TD
></TR
></TABLE
><P
>Apart from general aliases, it contains useful aliases which make commands work even if you misspell them. We will discuss aliases in <A
HREF="sect_03_05.html#sect_03_05_02"
>Section 3.5.2</A
>. This file contains a function, <B
CLASS="command"
>pskill</B
>; functions will be studied in detail in <A
HREF="chap_11.html"
>Chapter 11</A
>.</P
></DIV
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="sect_03_01_02"
></A
>3.1.2. Individual user configuration files</H2
><DIV
CLASS="note"
><P
></P
><TABLE
CLASS="note"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="../images/note.gif"
HSPACE="5"
ALT="Note"></TD
><TH
ALIGN="LEFT"
VALIGN="CENTER"
><B
>I don't have these files?!</B
></TH
></TR
><TR
><TD
>&nbsp;</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>These files might not be in your home directory by default; create them if needed.</P
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="sect_03_01_02_01"
></A
>3.1.2.1. ~/.bash_profile</H3
><P
>This is the preferred configuration file for configuring user environments individually. In this file, users can add extra configuration options or change default settings:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="screen"
>&#13;<TT
CLASS="prompt"
>franky~&#62;</TT
> <B
CLASS="command"
>cat <TT
CLASS="filename"
>.bash_profile</TT
></B
>
#################################################################
# #
# .bash_profile file #
# #
# Executed from the bash shell when you log in. #
# #
#################################################################
source ~/.bashrc
source ~/.bash_login
case "$OS" in
IRIX)
stty sane dec
stty erase
;;
# SunOS)
# stty erase
# ;;
*)
stty sane
;;
esac
</PRE
></FONT
></TD
></TR
></TABLE
><P
>This user configures the backspace character for login on different operating systems. Apart from that, the user's <TT
CLASS="filename"
>.bashrc</TT
> and <TT
CLASS="filename"
>.bash_login</TT
> are read.</P
></DIV
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="sect_03_01_02_02"
></A
>3.1.2.2. ~/.bash_login</H3
><P
>This file contains specific settings that are normally only executed when you log in to the system. In the example, we use it to configure the <B
CLASS="command"
>umask</B
> value and to show a list of connected users upon login. This user also gets the calendar for the current month:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="screen"
>&#13;#######################################################################
# #
# Bash_login file #
# #
# commands to perform from the bash shell at login time #
# (sourced from .bash_profile) #
# #
#######################################################################
# file protection
umask 002 # all to me, read to group and others
# miscellaneous
w
cal `date +"%m"` `date +"%Y"`
</PRE
></FONT
></TD
></TR
></TABLE
><P
>In the absence of <TT
CLASS="filename"
>~/.bash_profile</TT
>, this file will be read.</P
></DIV
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="sect_03_01_02_03"
></A
>3.1.2.3. ~/.profile</H3
><P
>In the absence of <TT
CLASS="filename"
>~/.bash_profile</TT
> and <TT
CLASS="filename"
>~/.bash_login</TT
>, <TT
CLASS="filename"
>~/.profile</TT
> is read. It can hold the same configurations, which are then also accessible by other shells. Mind that other shells might not understand the Bash syntax.</P
></DIV
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="sect_03_01_02_04"
></A
>3.1.2.4. ~/.bashrc</H3
><P
>Today, it is more common to use a non-login shell, for instance when logged in graphically using X terminal windows. Upon opening such a window, the user does not have to provide a user name or password; no authentication is done. Bash searches for <TT
CLASS="filename"
>~/.bashrc</TT
> when this happens, so it is referred to in the files read upon login as well, which means you don't have to enter the same settings in multiple files.</P
><P
>In this user's <TT
CLASS="filename"
>.bashrc</TT
> a couple of aliases are defined and variables for specific programs are set after the system-wide <TT
CLASS="filename"
>/etc/bashrc</TT
> is read:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="screen"
>&#13;<TT
CLASS="prompt"
>franky ~&#62;</TT
> <B
CLASS="command"
>cat <TT
CLASS="filename"
>.bashrc</TT
></B
>
# /home/franky/.bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# shell options
set -o noclobber
# my shell variables
export PS1="\[\033[1;44m\]\u \w\[\033[0m\] "
export PATH="$PATH:~/bin:~/scripts"
# my aliases
alias cdrecord='cdrecord -dev 0,0,0 -speed=8'
alias ss='ssh octarine'
alias ll='ls -la'
# mozilla fix
MOZILLA_FIVE_HOME=/usr/lib/mozilla
LD_LIBRARY_PATH=/usr/lib/mozilla:/usr/lib/mozilla/plugins
MOZ_DIST_BIN=/usr/lib/mozilla
MOZ_PROGRAM=/usr/lib/mozilla/mozilla-bin
export MOZILLA_FIVE_HOME LD_LIBRARY_PATH MOZ_DIST_BIN MOZ_PROGRAM
# font fix
alias xt='xterm -bg black -fg white &#38;'
# BitchX settings
export IRCNAME="frnk"
# THE END
<TT
CLASS="prompt"
>franky ~&#62;</TT
>
</PRE
></FONT
></TD
></TR
></TABLE
><P
>More examples can be found in the Bash package. Remember that sample files might need changes in order to work in your environment.</P
><P
>Aliases are discussed in <A
HREF="sect_03_05.html"
>Section 3.5</A
>.</P
></DIV
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="sect_03_01_02_05"
></A
>3.1.2.5. ~/.bash_logout</H3
><P
>This file contains specific instructions for the logout procedure. In the example, the terminal window is cleared upon logout. This is useful for remote connections, which will leave a clean window after closing them.</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="screen"
>&#13;<TT
CLASS="prompt"
>franky ~&#62;</TT
> <B
CLASS="command"
>cat <TT
CLASS="filename"
>.bash_logout</TT
></B
>
#######################################################################
# #
# Bash_logout file #
# #
# commands to perform from the bash shell at logout time #
# #
#######################################################################
clear
<TT
CLASS="prompt"
>franky ~&#62;</TT
>
</PRE
></FONT
></TD
></TR
></TABLE
></DIV
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="sect_03_01_03"
></A
>3.1.3. Changing shell configuration files</H2
><P
>When making changes to any of the above files, users have to either reconnect to the system or <B
CLASS="command"
>source</B
> the altered file for the changes to take effect. By interpreting the script this way, changes are applied to the current shell session:</P
><DIV
CLASS="figure"
><A
NAME="AEN1878"
></A
><P
><B
>Figure 3-1. Different prompts for different users</B
></P
><DIV
CLASS="mediaobject"
><P
><IMG
SRC="images/prompt.png"></P
></DIV
></DIV
><P
>Most shell scripts execute in a private environment: variables are not inherited by child processes unless they are exported by the parent shell. Sourcing a file containing shell commands is a way of applying changes to your own environment and setting variables in the current shell.</P
><P
>This example also demonstrates the use of different prompt settings by different users. In this case, red means danger. When you have a green prompt, don't worry too much.</P
><P
>Note that <B
CLASS="command"
>source <TT
CLASS="filename"
>resourcefile</TT
></B
> is the same as <B
CLASS="command"
>. <TT
CLASS="filename"
>resourcefile</TT
></B
>.</P
><P
>Should you get lost in all these configuration files, and find yourself confronted with settings of which the origin is not clear, use <B
CLASS="command"
>echo</B
> statements, just like for debugging scripts; see <A
HREF="sect_02_03.html#sect_02_03_02"
>Section 2.3.2</A
>. You might add lines like this:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="screen"
>&#13;echo "Now executing .bash_profile.."
</PRE
></FONT
></TD
></TR
></TABLE
><P
>or like this:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="screen"
>&#13;echo "Now setting PS1 in .bashrc:"
export PS1="[some value]"
echo "PS1 is now set to $PS1"
</PRE
></FONT
></TD
></TR
></TABLE
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="chap_03.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="sect_03_02.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>The Bash environment</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="chap_03.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Variables</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>