1 | <sect1 id="postlfs-config-profile" xreflabel="The Bash Shell Startup Files">
|
---|
2 | <?dbhtml filename="profile.html"?>
|
---|
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. Each file has a specific use and
|
---|
8 | may affect login and interactive environments differently. The files in
|
---|
9 | the <filename class="directory">/etc</filename> directory generally provide global
|
---|
10 | settings. If an equivalent file exists in your home directory it may
|
---|
11 | override the global settings.
|
---|
12 | </para>
|
---|
13 |
|
---|
14 | <para>An interactive login shell is started after a successful login, using
|
---|
15 | <filename>/bin/login</filename>, by reading the <filename>/etc/passwd</filename>
|
---|
16 | file. This shell invocation normally reads <filename>/etc/profile</filename>
|
---|
17 | and its private equivalent <filename>~/.bash_profile</filename> upon startup.</para>
|
---|
18 |
|
---|
19 | <para>An interactive non-login shell is normally started at the command-line
|
---|
20 | (e.g. <prompt>[prompt]$</prompt><command>/bin/bash</command>) or by the
|
---|
21 | <command>/bin/su</command> command. An interactive non-login shell is also
|
---|
22 | started with a terminal program such as <command>xterm</command> or
|
---|
23 | <command>konsole</command> from within a graphical environment. This type of
|
---|
24 | shell invocation normally copies the parent environment and then reads the
|
---|
25 | user's <filename>~/.bashrc</filename> file for additional startup configuration
|
---|
26 | instructions.</para>
|
---|
27 |
|
---|
28 | <para>A non-interactive shell is usually present when a shell script is
|
---|
29 | running. It is non-interactive because it is processing a script and not
|
---|
30 | waiting for user input between commands. For these shell invocatinos, only
|
---|
31 | the environment inherited form the parent shell is used.</para>
|
---|
32 |
|
---|
33 | <para> The file <filename>~/.bash_logout</filename> is not used for an
|
---|
34 | invocation of the shell. It is read and executed when a user exits from an
|
---|
35 | interactive login shell.</para>
|
---|
36 |
|
---|
37 | <para>To the standard files, we also add <filename>/etc/bashrc</filename>
|
---|
38 | which is called from the user's <filename>~/.bashrc</filename> for
|
---|
39 | system wide initialization of non-login shells.</para>
|
---|
40 |
|
---|
41 | <para>For more information see <command>info bash</command> --
|
---|
42 | <emphasis role="strong">Nodes: Bash Startup Files and Interactive
|
---|
43 | Shells.</emphasis></para>
|
---|
44 |
|
---|
45 | <sect2>
|
---|
46 | <title><filename>/etc/profile</filename></title>
|
---|
47 |
|
---|
48 | <para>Here is a base <filename>/etc/profile</filename>. This file starts by
|
---|
49 | setting up some helper functions and some basic parameters. It specifies some
|
---|
50 | <filename>bash</filename> history parameters and, for security purposes,
|
---|
51 | disables keeping a permanent history file for the root user. It also sets a
|
---|
52 | default user prompt. It then calls small, single purpose scripts in the
|
---|
53 | <filename class='directory'>/etc/profile.d</filename> directory to provide most
|
---|
54 | initialization. </para>
|
---|
55 |
|
---|
56 | <para>For more information on the escape sequences you can use for your prompt
|
---|
57 | (e.g. the <envar>PS1</envar> environment variable) see <command>info
|
---|
58 | bash</command> -- <emphasis role="strong">Node: Printing a
|
---|
59 | Prompt.</emphasis></para>
|
---|
60 |
|
---|
61 | <screen><userinput><command>cat > /etc/profile << "EOF"</command>
|
---|
62 | # Begin /etc/profile
|
---|
63 | # Written for Beyond Linux From Scratch
|
---|
64 | # by James Robertson <jameswrobertson@earthlink.net>
|
---|
65 | # modifications by Dagmar d'Surreal <rivyqntzne@pbzpnfg.arg>
|
---|
66 |
|
---|
67 | # System wide environment variables and startup programs.
|
---|
68 |
|
---|
69 | # System wide aliases and functions should go in /etc/bashrc. Personal
|
---|
70 | # environment variables and startup programs should go into
|
---|
71 | # ~/.bash_profile. Personal aliases and functions should go into
|
---|
72 | # ~/.bashrc.
|
---|
73 |
|
---|
74 | # Functions to help us manage paths. Second argument is the name of the
|
---|
75 | # path variable to be modified (default: PATH)
|
---|
76 | pathremove () {
|
---|
77 | local IFS=':'
|
---|
78 | local NEWPATH
|
---|
79 | local DIR
|
---|
80 | local PATHVARIABLE=${2:-PATH}
|
---|
81 | for DIR in ${!PATHVARIABLE} ; do
|
---|
82 | if [ "$DIR" != "$1" ] ; then
|
---|
83 | NEWPATH=${NEWPATH:+$NEWPATH:}$DIR
|
---|
84 | fi
|
---|
85 | done
|
---|
86 | export $PATHVARIABLE="$NEWPATH"
|
---|
87 | }
|
---|
88 |
|
---|
89 | pathprepend () {
|
---|
90 | pathremove $1 $2
|
---|
91 | local PATHVARIABLE=${2:-PATH}
|
---|
92 | export $PATHVARIABLE="$1${!PATHVARIABLE:+:${!PATHVARIABLE}}"
|
---|
93 | }
|
---|
94 |
|
---|
95 | pathappend () {
|
---|
96 | pathremove $1 $2
|
---|
97 | local PATHVARIABLE=${2:-PATH}
|
---|
98 | export $PATHVARIABLE="${!PATHVARIABLE:+${!PATHVARIABLE}:}$1"
|
---|
99 | }
|
---|
100 |
|
---|
101 | if [ $EUID -eq 0 ] ; then
|
---|
102 | unset HISTFILE
|
---|
103 | fi
|
---|
104 |
|
---|
105 | # Setup some environment variables.
|
---|
106 | export HISTSIZE=1000
|
---|
107 | export HISTIGNORE="&:[bf]g:exit"
|
---|
108 | #export PS1="[\u@\h \w]\\$ "
|
---|
109 | export PS1='\u@\h:\w\$ '
|
---|
110 |
|
---|
111 | for script in /etc/profile.d/*.sh ; do
|
---|
112 | if [ -x $script ] ; then
|
---|
113 | . $script
|
---|
114 | fi
|
---|
115 | done
|
---|
116 |
|
---|
117 | # Now to clean up after ourselves
|
---|
118 | unset pathremove pathprepend pathappend
|
---|
119 |
|
---|
120 | # End /etc/profile
|
---|
121 | <command>EOF</command></userinput></screen>
|
---|
122 |
|
---|
123 | <para>Now create the <filename class='directory'>/etc/profile.d</filename> directory.</para>
|
---|
124 |
|
---|
125 | <screen><userinput><command>install --directory --mode=0755 --owner=root --group=root /etc/profile.d</command></userinput></screen>
|
---|
126 |
|
---|
127 | <sect3>
|
---|
128 | <title><filename>/etc/profile.d/dircolors.sh</filename></title>
|
---|
129 |
|
---|
130 | <para>This script uses the <filename>~/.dircolors</filename> and
|
---|
131 | <filename>/etc/dircolors</filename> files to control the colors of file names in a
|
---|
132 | directory listing. They control colorized output of things like <command>ls
|
---|
133 | --color</command>. The explaination of how to initialize these files is at the
|
---|
134 | end of this section. </para>
|
---|
135 |
|
---|
136 |
|
---|
137 | <screen><userinput><command>cat > /etc/profile.d/dircolors.sh << "EOF"</command>
|
---|
138 | # Setup for /bin/ls to support color, the alias is in /etc/bashrc.
|
---|
139 | if [ -f "/etc/dircolors" ] ; then
|
---|
140 | eval $(dircolors -b /etc/dircolors)
|
---|
141 |
|
---|
142 | if [ -f "$HOME/.dircolors" ] ; then
|
---|
143 | eval $(dircolors -b $HOME/.dircolors)
|
---|
144 | fi
|
---|
145 | fi
|
---|
146 | alias ls='ls --color=auto'
|
---|
147 | <command>EOF</command></userinput></screen>
|
---|
148 | </sect3>
|
---|
149 |
|
---|
150 |
|
---|
151 | <sect3>
|
---|
152 | <title><filename>/etc/profile.d/extrapaths.sh</filename></title>
|
---|
153 |
|
---|
154 | <para>This script adds several useful paths to the <envar>PATH</envar> and
|
---|
155 | <envar>PKG_CONFIG_PATH</envar> environment variables. If you want, you can uncomment
|
---|
156 | the last section to put a dot at the end of your path. This will allow executables in the
|
---|
157 | current working directory to be executed without specifiying a ./, however
|
---|
158 | you are warned that this is generally considered a security hazard.</para>
|
---|
159 |
|
---|
160 | <screen><userinput><command>cat > /etc/profile.d/extrapaths.sh << "EOF"</command>
|
---|
161 | if [ -d /usr/local/lib/pkgconfig ] ; then
|
---|
162 | pathappend /usr/local/lib/pkgconfig PKG_CONFIG_PATH
|
---|
163 | fi
|
---|
164 | if [ -d /usr/local/bin ]; then
|
---|
165 | pathprepend /usr/local/bin
|
---|
166 | fi
|
---|
167 | if [ -d /usr/local/sbin -a $EUID -eq 0 ]; then
|
---|
168 | pathprepend /usr/local/sbin
|
---|
169 | fi
|
---|
170 | for directory in $(find /opt/*/lib/pkgconfig -type d); do
|
---|
171 | pathappend $directory PKG_CONFIG_PATH
|
---|
172 | done
|
---|
173 | for directory in $(find /opt/*/bin -type d); do
|
---|
174 | pathappend $directory
|
---|
175 | done
|
---|
176 | if [ -d ~/bin ]; then
|
---|
177 | pathprepend ~/bin
|
---|
178 | fi
|
---|
179 | #if [ $EUID -gt 99 ]; then
|
---|
180 | # pathappend .
|
---|
181 | #fi
|
---|
182 | <command>EOF</command></userinput></screen>
|
---|
183 | </sect3>
|
---|
184 |
|
---|
185 | <sect3>
|
---|
186 | <title><filename>/etc/profile.d/readline.sh</filename></title>
|
---|
187 |
|
---|
188 | <para>This script sets up the default <filename>inputrc</filename> configuration file.
|
---|
189 | If the user does not have individual settings, it uses the global file.</para>
|
---|
190 |
|
---|
191 | <screen><userinput><command>cat > /etc/profile.d/readline.sh << "EOF"</command>
|
---|
192 | # Setup the INPUTRC environment variable.
|
---|
193 | if [ -z "$INPUTRC" -a ! -f "$HOME/.inputrc" ] ; then
|
---|
194 | INPUTRC=/etc/inputrc
|
---|
195 | fi
|
---|
196 | export INPUTRC
|
---|
197 | <command>EOF</command></userinput></screen>
|
---|
198 | </sect3>
|
---|
199 |
|
---|
200 | <sect3>
|
---|
201 | <title><filename>/etc/profile.d/tinker-term.sh</filename></title>
|
---|
202 |
|
---|
203 | <para>Some applications need a specific <envar>TERM</envar> setting to support color.</para>
|
---|
204 |
|
---|
205 | <screen><userinput><command>cat > /etc/profile.d/tinker-term.sh << "EOF"</command>
|
---|
206 | # This will tinker with the value of TERM in order to convince certain apps
|
---|
207 | # that we can, indeed, display color in their window.
|
---|
208 |
|
---|
209 | if [ -n "$COLORTERM" ]; then
|
---|
210 | export TERM=xterm-color
|
---|
211 | fi
|
---|
212 |
|
---|
213 | if [ "$TERM" = "xterm" ]; then
|
---|
214 | export TERM=xterm-color
|
---|
215 | fi
|
---|
216 | <command>EOF</command></userinput></screen>
|
---|
217 | </sect3>
|
---|
218 |
|
---|
219 | <sect3>
|
---|
220 | <title><filename>/etc/profile.d/umask.sh</filename></title>
|
---|
221 |
|
---|
222 | <para>Setting the <command>umask</command> value is important for security. Here
|
---|
223 | we turn off the default group write permissions for system users and when the
|
---|
224 | user name and group name are not the same.</para>
|
---|
225 |
|
---|
226 | <screen><userinput><command>cat > /etc/profile.d/umask.sh << "EOF"</command>
|
---|
227 | # By default we want the umask to get set.
|
---|
228 | if [ "$(id -gn)" = "$(id -un)" -a $EUID -gt 99 ] ; then
|
---|
229 | umask 002
|
---|
230 | else
|
---|
231 | umask 022
|
---|
232 | fi
|
---|
233 | <command>EOF</command></userinput></screen>
|
---|
234 | </sect3>
|
---|
235 |
|
---|
236 | <sect3>
|
---|
237 | <title><filename>/etc/profile.d/X.sh</filename></title>
|
---|
238 |
|
---|
239 | <para>If <application>X</application> is installed, we also update the <envar>PATH</envar>
|
---|
240 | and <envar>PKG_CONFIG_PATH</envar> variables.</para>
|
---|
241 |
|
---|
242 | <screen><userinput><command>cat > /etc/profile.d/X.sh << "EOF"</command>
|
---|
243 | if [ -x /usr/X11R6/bin/X ]; then
|
---|
244 | pathappend /usr/X11R6/bin
|
---|
245 | fi
|
---|
246 | if [ -d /usr/X11R6/lib/pkgconfig ] ; then
|
---|
247 | pathappend /usr/X11R6/lib/pkgconfig PKG_CONFIG_PATH
|
---|
248 | fi
|
---|
249 | <command>EOF</command></userinput></screen>
|
---|
250 | </sect3>
|
---|
251 |
|
---|
252 | <sect3>
|
---|
253 | <title><filename>/etc/profile.d/xterm-titlebars.sh</filename></title>
|
---|
254 |
|
---|
255 | <para>This script shows an example of a different way of setting the prompt. The normal
|
---|
256 | variable, <envar>PS1</envar>, is supplemented by <envar>PROMPT_COMMAND</envar>.
|
---|
257 | If set, the value of <envar>PROMPT_COMMAND</envar> is executed as a command prior to
|
---|
258 | issuing each primary prompt. </para>
|
---|
259 |
|
---|
260 | <screen><userinput><command>cat > /etc/profile.d/xterm-titlebars.sh << "EOF"</command>
|
---|
261 | # The substring match ensures this will work for "xterm" and "xterm-xfree86".
|
---|
262 | if [ "${TERM:0:5}" = "xterm" ]; then
|
---|
263 | PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME} : ${PWD}\007"'
|
---|
264 | export PROMPT_COMMAND
|
---|
265 | fi
|
---|
266 | <command>EOF</command></userinput></screen>
|
---|
267 |
|
---|
268 | <para>Other initialization can easily be added to the <filename>profile</filename>
|
---|
269 | by adding additional scripts to the
|
---|
270 | <filename class='directory'>/etc/profile.d</filename> directory.</para>
|
---|
271 | </sect3>
|
---|
272 | </sect2>
|
---|
273 |
|
---|
274 | <sect2>
|
---|
275 | <title><filename>/etc/bashrc</filename></title>
|
---|
276 | <para>Here is a base <filename>/etc/bashrc</filename>. Comments in the
|
---|
277 | file should explain everything you need.</para>
|
---|
278 |
|
---|
279 | <screen><userinput><command>cat > /etc/bashrc << "EOF"</command>
|
---|
280 | # Begin /etc/bashrc
|
---|
281 | # Written for Beyond Linux From Scratch
|
---|
282 | # by James Robertson <jameswrobertson@earthlink.net>
|
---|
283 |
|
---|
284 | # System wide aliases and functions.
|
---|
285 |
|
---|
286 | # System wide environment variables and startup programs should go into
|
---|
287 | # /etc/profile. Personal environment variables and startup programs
|
---|
288 | # should go into ~/.bash_profile. Personal aliases and functions should
|
---|
289 | # go into ~/.bashrc
|
---|
290 |
|
---|
291 | # Provides a colored /bin/ls command. Used in conjunction with code in
|
---|
292 | # /etc/profile.
|
---|
293 |
|
---|
294 | alias ls='ls --color=auto'
|
---|
295 |
|
---|
296 | # Provides prompt for non-login shells, specifically shells started
|
---|
297 | # in the <application>X</application> environment. [Review the LFS archive thread titled
|
---|
298 | # PS1 Environment Variable for a great case study behind this script addendum.]
|
---|
299 |
|
---|
300 | #export PS1="[\u@\h \w]\\$ "
|
---|
301 | export PS1='\u@\h:\w\$ '
|
---|
302 |
|
---|
303 | # End /etc/bashrc
|
---|
304 | <command>EOF</command></userinput></screen>
|
---|
305 | </sect2>
|
---|
306 |
|
---|
307 |
|
---|
308 | <sect2>
|
---|
309 | <title><filename>~/.bash_profile</filename></title>
|
---|
310 |
|
---|
311 | <para>Here is a base <filename>~/.bash_profile</filename>. If you want each
|
---|
312 | new user to have this file automatically, just change the output of
|
---|
313 | the command to <filename>/etc/skel/.bash_profile</filename> and check the
|
---|
314 | permissions after the command is run. You can then copy
|
---|
315 | <filename>/etc/skel/.bash_profile</filename> to the home directories of already
|
---|
316 | existing users, including root, and set the owner and group appropriately.
|
---|
317 | </para>
|
---|
318 |
|
---|
319 | <screen><userinput><command>cat > ~/.bash_profile << "EOF"</command>
|
---|
320 | # Begin ~/.bash_profile
|
---|
321 | # Written for Beyond Linux From Scratch
|
---|
322 | # by James Robertson <jameswrobertson@earthlink.net>
|
---|
323 | # updated by Bruce Dubbs <bdubbs@linuxfromscratch.org>
|
---|
324 |
|
---|
325 | # Personal environment variables and startup programs.
|
---|
326 |
|
---|
327 | # Personal aliases and functions should go in ~/.bashrc. System wide
|
---|
328 | # environment variables and startup programs are in /etc/profile.
|
---|
329 | # System wide aliases and functions are in /etc/bashrc.
|
---|
330 |
|
---|
331 | append () {
|
---|
332 | # First remove the directory
|
---|
333 | local IFS=':'
|
---|
334 | local NEWPATH
|
---|
335 | for DIR in $PATH; do
|
---|
336 | if [ "$DIR" != "$1" ]; then
|
---|
337 | NEWPATH = ${NEWPATH:+$NEWPATH:}$DIR
|
---|
338 | fi
|
---|
339 | done
|
---|
340 |
|
---|
341 | # Then append the directory
|
---|
342 | export PATH=$NEWPATH:$1
|
---|
343 | }
|
---|
344 |
|
---|
345 | if [ -f "$HOME/.bashrc" ] ; then
|
---|
346 | source $HOME/.bashrc
|
---|
347 | fi
|
---|
348 |
|
---|
349 | if [ -d "$HOME/bin" ] ; then
|
---|
350 | append $HOME/bin
|
---|
351 | fi
|
---|
352 |
|
---|
353 | unset append
|
---|
354 |
|
---|
355 | # End ~/.bash_profile
|
---|
356 | <command>EOF</command></userinput></screen>
|
---|
357 | </sect2>
|
---|
358 |
|
---|
359 | <sect2>
|
---|
360 | <title><filename>~/.bashrc</filename></title>
|
---|
361 |
|
---|
362 | <para>Here is a base <filename>~/.bashrc</filename>. The comments and
|
---|
363 | instructions for using <filename class="directory">/etc/skel</filename> for
|
---|
364 | <filename>.bash_profile</filename> above also apply here. Only the target file
|
---|
365 | names are different.</para>
|
---|
366 |
|
---|
367 | <screen><userinput><command>cat > ~/.bashrc << "EOF"</command>
|
---|
368 | # Begin ~/.bashrc
|
---|
369 | # Written for Beyond Linux From Scratch
|
---|
370 | # by James Robertson <jameswrobertson@earthlink.net>
|
---|
371 |
|
---|
372 | # Personal aliases and functions.
|
---|
373 |
|
---|
374 | # Personal environment variables and startup programs should go in
|
---|
375 | # ~/.bash_profile. System wide environment variables and startup
|
---|
376 | # programs are in /etc/profile. System wide aliases and functions are
|
---|
377 | # in /etc/bashrc.
|
---|
378 |
|
---|
379 | if [ -f "/etc/bashrc" ] ; then
|
---|
380 | source /etc/bashrc
|
---|
381 | fi
|
---|
382 |
|
---|
383 | # End ~/.bashrc
|
---|
384 | <command>EOF</command></userinput></screen>
|
---|
385 | </sect2>
|
---|
386 |
|
---|
387 |
|
---|
388 | <sect2>
|
---|
389 | <title><filename>~/.bash_logout</filename></title>
|
---|
390 |
|
---|
391 | <para>This is an empty <filename>~/.bash_logout</filename> that can be used as
|
---|
392 | a template. You will notice that the base <filename>~/.bash_logout</filename>
|
---|
393 | does not include a <userinput>clear</userinput> command. This is because the
|
---|
394 | clear is handled in the <filename>/etc/issue</filename> file.</para>
|
---|
395 |
|
---|
396 | <screen><userinput><command>cat > ~/.bash_logout << "EOF"</command>
|
---|
397 | # Begin ~/.bash_logout
|
---|
398 | # Written for Beyond Linux From Scratch
|
---|
399 | # by James Robertson <jameswrobertson@earthlink.net>
|
---|
400 |
|
---|
401 | # Personal items to perform on logout.
|
---|
402 |
|
---|
403 | # End ~/.bash_logout
|
---|
404 | <command>EOF</command></userinput></screen>
|
---|
405 | </sect2>
|
---|
406 |
|
---|
407 |
|
---|
408 | <sect2>
|
---|
409 | <title><filename>/etc/dircolors</filename></title>
|
---|
410 |
|
---|
411 | <para> If you want to use the <filename>dircolors</filename> capability then
|
---|
412 | run the following command. The <filename class="directory">/etc/skel</filename>
|
---|
413 | setup steps seen above also can be used here to provide a
|
---|
414 | <filename>.dircolors</filename> file when a new user is set up. As before, just
|
---|
415 | change the output file name on the following command and assure the
|
---|
416 | permissions, owner and group are correct on the files created and/or copied.
|
---|
417 | </para>
|
---|
418 |
|
---|
419 | <screen><userinput><command>dircolors -p > /etc/dircolors</command></userinput></screen>
|
---|
420 |
|
---|
421 | <para>If you wish to customize the colors used for different file types, you can
|
---|
422 | edit the <filename>/etc/dircolors</filename> file. The instructions for setting
|
---|
423 | the colors are embedded in the file.</para>
|
---|
424 |
|
---|
425 |
|
---|
426 | <para>Finally, Ian Macdonald has written an excellent collection of tips and
|
---|
427 | tricks to enhance your shell environment. You can read it online at
|
---|
428 | <ulink
|
---|
429 | url="http://www.caliban.org/bash/index.shtml">http://www.caliban.org/bash/index.shtml</ulink></para>
|
---|
430 | </sect2>
|
---|
431 | </sect1>
|
---|