1 | <sect1 id="postlfs-config-profile">
|
---|
2 | <?dbhtml filename="profile.html" dir="postlfs"?>
|
---|
3 | <title>The Bash Shell Startup Files</title>
|
---|
4 |
|
---|
5 | <para>The shell program <filename>/bin/bash</filename> (hereafter
|
---|
6 | referred to as just "the shell") uses a collection of startup files to
|
---|
7 | help create an environment to run in. Each file has a specific use and
|
---|
8 | may affect login and interactive environments differently.</para>
|
---|
9 |
|
---|
10 | <para>An interactive login shell is started after a successful login by
|
---|
11 | <filename>/bin/login</filename> by reading the
|
---|
12 | <filename>/etc/passwd</filename> file. An
|
---|
13 | interactive non-login shell is started at the command line (e.g.
|
---|
14 | <prompt>[prompt]$</prompt><command>/bin/bash</command>). A non-interactive
|
---|
15 | shell is usually present when a shell script is running. It is non-interactive
|
---|
16 | because it is processing a script and not waiting for user input between
|
---|
17 | commands.</para>
|
---|
18 |
|
---|
19 | <para>For more information see <command>info bash</command> --
|
---|
20 | <emphasis role="strong">Nodes: Bash Startup Files and Interactive
|
---|
21 | Shells.</emphasis></para>
|
---|
22 |
|
---|
23 | <para>The following files are needed to make sure that the correct
|
---|
24 | environment is read for each of the ways the shell can be invoked:
|
---|
25 | <filename>/etc/profile</filename>, <filename>/etc/bashrc</filename>,
|
---|
26 | <filename>~/.bash_profile</filename>, and
|
---|
27 | <filename>~/.bashrc</filename>. The file
|
---|
28 | <filename>~/.bash_logout</filename> is not used for an invocation of the
|
---|
29 | shell. It is read by the shell when a user logouts of the system. The
|
---|
30 | files <filename>/etc/profile</filename> and
|
---|
31 | <filename>~/.bash_profile</filename> are read when the shell is invoked
|
---|
32 | as a interactive login shell. The file <filename>~/.bashrc</filename>
|
---|
33 | is read when the shell is invoked as an interactive non-login
|
---|
34 | shell.</para>
|
---|
35 |
|
---|
36 | <para>Here is a base <filename>/etc/profile</filename>. Comments in the
|
---|
37 | file should explain everything you need. For more information on the
|
---|
38 | escape sequences you can use for your prompt (e.g. the
|
---|
39 | <envar>PS1</envar> environment variable) see <command>info
|
---|
40 | bash</command> -- <emphasis role="strong">Node: Printing a
|
---|
41 | Prompt.</emphasis></para>
|
---|
42 |
|
---|
43 | <screen><userinput><command>cat > /etc/profile << "EOF"</command>
|
---|
44 | # Begin /etc/profile
|
---|
45 | # Written for Beyond Linux From Scratch
|
---|
46 | # by James Robertson <jameswrobertson@earthlink.net>
|
---|
47 |
|
---|
48 | # System wide environment variables and startup programs.
|
---|
49 |
|
---|
50 | # System wide aliases and functions should go in /etc/bashrc. Personal
|
---|
51 | # environment variables and startup programs should go into
|
---|
52 | # ~/.bash_profile. Personal aliases and functions should go into
|
---|
53 | # ~/.bashrc.
|
---|
54 |
|
---|
55 | # Function to help us manage paths
|
---|
56 | pathman () {
|
---|
57 | if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
|
---|
58 | if [ "$2" = "last" ] ; then
|
---|
59 | PATH=$PATH:$1
|
---|
60 | else
|
---|
61 | PATH=$1:$PATH
|
---|
62 | fi
|
---|
63 | fi
|
---|
64 | }
|
---|
65 |
|
---|
66 | # Add to the standard path.
|
---|
67 | if [ $(id -u) = 0 ] ; then
|
---|
68 | if [ -d "/usr/local/sbin" ] ; then
|
---|
69 | pathman /usr/local/sbin last
|
---|
70 | fi
|
---|
71 | fi
|
---|
72 |
|
---|
73 | if [ $(id -u) != 0 ] ; then
|
---|
74 | if [ -d "/usr/local/bin" ] ; then
|
---|
75 | pathman /usr/local/bin last
|
---|
76 | fi
|
---|
77 | fi
|
---|
78 |
|
---|
79 | if [ -d "/usr/X11R6/bin" ] ; then
|
---|
80 | pathman /usr/X11R6/bin last
|
---|
81 | fi
|
---|
82 |
|
---|
83 | # Setup some environment variables.
|
---|
84 | HISTSIZE=1000
|
---|
85 | PS1="[\u@\h \w]\\$ "
|
---|
86 |
|
---|
87 | # Setup the INPUTRC environment variable.
|
---|
88 | if [ -z "$INPUTRC" -a ! -f "$HOME/.inputrc" ] ; then
|
---|
89 | INPUTRC=/etc/inputrc
|
---|
90 | fi
|
---|
91 |
|
---|
92 | # Setup for /bin/ls to support color, the alias is in /etc/bashrc.
|
---|
93 | if [ -f "/etc/dircolors" ] ; then
|
---|
94 | eval $(dircolors -b /etc/dircolors)
|
---|
95 |
|
---|
96 | if [ -f "$HOME/.dircolors" ] ; then
|
---|
97 | eval $(dircolors -b $HOME/.dircolors)
|
---|
98 | fi
|
---|
99 | fi
|
---|
100 |
|
---|
101 | export PATH HISTSIZE PS1 LS_COLORS INPUTRC
|
---|
102 |
|
---|
103 | # End /etc/profile
|
---|
104 | <command>EOF</command></userinput></screen>
|
---|
105 |
|
---|
106 | <para>Here is a base <filename>/etc/bashrc</filename>. Comments in the
|
---|
107 | file should explain everything you need.</para>
|
---|
108 |
|
---|
109 | <screen><userinput><command>cat > /etc/bashrc << "EOF"</command>
|
---|
110 | # Begin /etc/bashrc
|
---|
111 | # Written for Beyond Linux From Scratch
|
---|
112 | # by James Robertson <jameswrobertson@earthlink.net>
|
---|
113 |
|
---|
114 | # System wide aliases and functions.
|
---|
115 |
|
---|
116 | # System wide environment variables and startup programs should go into
|
---|
117 | # /etc/profile. Personal environment variables and startup programs
|
---|
118 | # should go into ~/.bash_profile. Personal aliases and functions should
|
---|
119 | # go into ~/.bashrc
|
---|
120 |
|
---|
121 | # By default we want the umask to get set.
|
---|
122 | # Even for non-interactive and non-login shells.
|
---|
123 | if [ "$(id -gn)" = "$(id -un)" -a $(id -u) -gt 99 ] ; then
|
---|
124 | umask 002
|
---|
125 | else
|
---|
126 | umask 022
|
---|
127 | fi
|
---|
128 |
|
---|
129 | # Provides a colored /bin/ls command. Used in conjunction with code in
|
---|
130 | # /etc/profile.
|
---|
131 | alias ls='ls --color=auto'
|
---|
132 |
|
---|
133 | # End /etc/bashrc
|
---|
134 | <command>EOF</command></userinput></screen>
|
---|
135 |
|
---|
136 | <para>Here is a base <filename>~/.bash_profile</filename>. Comments in
|
---|
137 | the file should explain everything you need.</para>
|
---|
138 |
|
---|
139 | <screen><userinput><command>cat > ~/.bash_profile << "EOF"</command>
|
---|
140 | # Begin ~/.bash_profile
|
---|
141 | # Written for Beyond Linux From Scratch
|
---|
142 | # by James Robertson <jameswrobertson@earthlink.net>
|
---|
143 |
|
---|
144 | # Personal environment variables and startup programs.
|
---|
145 |
|
---|
146 | # Personal aliases and functions should go in ~/.bashrc. System wide
|
---|
147 | # environment variables and startup programs are in /etc/profile.
|
---|
148 | # System wide aliases and functions are in /etc/bashrc.
|
---|
149 |
|
---|
150 | if [ -f "$HOME/.bashrc" ] ; then
|
---|
151 | source $HOME/.bashrc
|
---|
152 | fi
|
---|
153 |
|
---|
154 | if [ -d "$HOME/bin" ] ; then
|
---|
155 | pathman $HOME/bin last
|
---|
156 | fi
|
---|
157 |
|
---|
158 | export PATH
|
---|
159 |
|
---|
160 | # End ~/.bash_profile
|
---|
161 | <command>EOF</command></userinput></screen>
|
---|
162 |
|
---|
163 | <para>Here is a base <filename>~/.bashrc</filename>. Comments in the
|
---|
164 | file should explain everything you need.</para>
|
---|
165 |
|
---|
166 | <screen><userinput><command>cat > ~/.bashrc << "EOF"</command>
|
---|
167 | # Begin ~/.bashrc
|
---|
168 | # Written for Beyond Linux From Scratch
|
---|
169 | # by James Robertson <jameswrobertson@earthlink.net>
|
---|
170 |
|
---|
171 | # Personal aliases and functions.
|
---|
172 |
|
---|
173 | # Personal environment variables and startup programs should go in
|
---|
174 | # ~/.bash_profile. System wide environment variables and startup
|
---|
175 | # programs are in /etc/profile. System wide aliases and functions are
|
---|
176 | # in /etc/bashrc.
|
---|
177 |
|
---|
178 | if [ -f "/etc/bashrc" ] ; then
|
---|
179 | source /etc/bashrc
|
---|
180 | fi
|
---|
181 |
|
---|
182 | # End ~/.bashrc
|
---|
183 | <command>EOF</command></userinput></screen>
|
---|
184 |
|
---|
185 | <para>Here is a base <filename>~/.bash_logout</filename>. Comments in
|
---|
186 | the file should explain everything you need. You will notice that the
|
---|
187 | base <filename>~/.bash_logout</filename> does not include a
|
---|
188 | <userinput>clear</userinput> command. This is because the clear is
|
---|
189 | handled in the <filename>/etc/issue</filename> file.</para>
|
---|
190 |
|
---|
191 | <screen><userinput><command>cat > ~/.bash_logout << "EOF"</command>
|
---|
192 | # Begin ~/.bash_logout
|
---|
193 | # Written for Beyond Linux From Scratch
|
---|
194 | # by James Robertson <jameswrobertson@earthlink.net>
|
---|
195 |
|
---|
196 | # Personal items to perform on logout.
|
---|
197 |
|
---|
198 | # End ~/.bash_logout
|
---|
199 | <command>EOF</command></userinput></screen>
|
---|
200 |
|
---|
201 | <para>If you want to use the <filename>/etc/dircolors</filename> or
|
---|
202 | <filename>~/.dircolors</filename> files called from
|
---|
203 | <filename>/etc/profile</filename>, then run the following:
|
---|
204 | <userinput>/bin/dircolors -p > /etc/dircolors</userinput> or
|
---|
205 | <userinput>/bin/dircolors -p > ~/.dircolors</userinput> respectively.
|
---|
206 | The file in the <filename>/etc</filename> directory should be used for
|
---|
207 | global settings and if one exists in your home directory then it will
|
---|
208 | overwrite the global settings. It might be a good idea to create a base
|
---|
209 | <filename>.dircolors</filename> file and place it in the
|
---|
210 | <filename>/etc/skel</filename> directory for new users.</para>
|
---|
211 |
|
---|
212 | <para>Ian Macdonald has written an excellent collection of tips and
|
---|
213 | tricks to enhance your shell environment. You can read it online at
|
---|
214 | <ulink
|
---|
215 | url="http://www.caliban.org/bash/index.shtml">http://www.caliban.org/bash/index.shtml</ulink></para>
|
---|
216 |
|
---|
217 | </sect1>
|
---|