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. The files in
|
---|
9 | the <filename>/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
|
---|
16 | <filename>/etc/passwd</filename> file. An
|
---|
17 | interactive non-login shell is started at the command line (e.g.
|
---|
18 | <prompt>[prompt]$</prompt><command>/bin/bash</command>). A non-interactive
|
---|
19 | shell is usually present when a shell script is running. It is non-interactive
|
---|
20 | because it is processing a script and not waiting for user input between
|
---|
21 | commands.</para>
|
---|
22 |
|
---|
23 | <para>For more information see <command>info bash</command> --
|
---|
24 | <emphasis role="strong">Nodes: Bash Startup Files and Interactive
|
---|
25 | Shells.</emphasis></para>
|
---|
26 |
|
---|
27 | <para>The following files are used to make sure that the correct
|
---|
28 | environment is established for each of the ways the shell can be invoked:
|
---|
29 | <filename>/etc/profile</filename> and its private equivalent
|
---|
30 | <filename>~/.bash_profile</filename>, and
|
---|
31 | <filename>/etc/bashrc</filename> (unofficial) and its private equivalent
|
---|
32 | <filename>~/.bashrc</filename>.
|
---|
33 | </para>
|
---|
34 |
|
---|
35 | <para>
|
---|
36 | The file <filename>~/.bash_logout</filename> is not used for an
|
---|
37 | invocation of the shell. It is read by the shell when a user logs out
|
---|
38 | of the system.</para>
|
---|
39 |
|
---|
40 | <para>The files <filename>/etc/profile</filename> and
|
---|
41 | <filename>~/.bash_profile</filename> are read when the shell is invoked
|
---|
42 | as an interactive login shell. The file <filename>~/.bashrc</filename>
|
---|
43 | is read when the shell is invoked as an interactive non-login
|
---|
44 | shell and it reads <filename>/etc/bashrc</filename> if it exists</para>
|
---|
45 |
|
---|
46 | <para>Also useful are the <filename>/etc/dircolors</filename> and
|
---|
47 | <filename>~/.dircolors</filename> files called from
|
---|
48 | <filename>/etc/profile</filename>. They control colorized output of
|
---|
49 | things like <emphasis>ls --color</emphasis>.
|
---|
50 | </para>
|
---|
51 |
|
---|
52 | <para>Here is a base <filename>/etc/profile</filename>. Comments in the
|
---|
53 | file should explain everything you need. For more information on the
|
---|
54 | escape sequences you can use for your prompt (e.g. the
|
---|
55 | <envar>PS1</envar> environment variable) see <command>info
|
---|
56 | bash</command> -- <emphasis role="strong">Node: Printing a
|
---|
57 | Prompt.</emphasis></para>
|
---|
58 |
|
---|
59 | <screen><userinput><command>cat > /etc/profile << "EOF"</command>
|
---|
60 | # Begin /etc/profile
|
---|
61 | # Written for Beyond Linux From Scratch
|
---|
62 | # by James Robertson <jameswrobertson@earthlink.net>
|
---|
63 |
|
---|
64 | # System wide environment variables and startup programs.
|
---|
65 |
|
---|
66 | # System wide aliases and functions should go in /etc/bashrc. Personal
|
---|
67 | # environment variables and startup programs should go into
|
---|
68 | # ~/.bash_profile. Personal aliases and functions should go into
|
---|
69 | # ~/.bashrc.
|
---|
70 |
|
---|
71 | # Function to help us manage paths
|
---|
72 | pathman () {
|
---|
73 | if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
|
---|
74 | if [ "$2" = "last" ] ; then
|
---|
75 | PATH=$PATH:$1
|
---|
76 | else
|
---|
77 | PATH=$1:$PATH
|
---|
78 | fi
|
---|
79 | fi
|
---|
80 | }
|
---|
81 |
|
---|
82 | # Add to the standard path.
|
---|
83 | if [ $(id -u) = 0 ] ; then
|
---|
84 | if [ -d "/usr/local/sbin" ] ; then
|
---|
85 | pathman /usr/local/sbin last
|
---|
86 | fi
|
---|
87 | fi
|
---|
88 |
|
---|
89 | if [ $(id -u) != 0 ] ; then
|
---|
90 | if [ -d "/usr/local/bin" ] ; then
|
---|
91 | pathman /usr/local/bin last
|
---|
92 | fi
|
---|
93 | fi
|
---|
94 |
|
---|
95 | if [ -d "/usr/X11R6/bin" ] ; then
|
---|
96 | pathman /usr/X11R6/bin last
|
---|
97 | fi
|
---|
98 |
|
---|
99 | # Setup some environment variables.
|
---|
100 | HISTSIZE=1000
|
---|
101 | PS1="[\u@\h \w]\\$ "
|
---|
102 |
|
---|
103 | # Setup the INPUTRC environment variable.
|
---|
104 | if [ -z "$INPUTRC" -a ! -f "$HOME/.inputrc" ] ; then
|
---|
105 | INPUTRC=/etc/inputrc
|
---|
106 | fi
|
---|
107 |
|
---|
108 | # Setup for /bin/ls to support color, the alias is in /etc/bashrc.
|
---|
109 | if [ -f "/etc/dircolors" ] ; then
|
---|
110 | eval $(dircolors -b /etc/dircolors)
|
---|
111 |
|
---|
112 | if [ -f "$HOME/.dircolors" ] ; then
|
---|
113 | eval $(dircolors -b $HOME/.dircolors)
|
---|
114 | fi
|
---|
115 | fi
|
---|
116 |
|
---|
117 | export PATH HISTSIZE PS1 LS_COLORS INPUTRC
|
---|
118 |
|
---|
119 | # End /etc/profile
|
---|
120 | <command>EOF</command></userinput></screen>
|
---|
121 |
|
---|
122 | <para>Here is a base <filename>/etc/bashrc</filename>. Comments in the
|
---|
123 | file should explain everything you need.</para>
|
---|
124 |
|
---|
125 | <screen><userinput><command>cat > /etc/bashrc << "EOF"</command>
|
---|
126 | # Begin /etc/bashrc
|
---|
127 | # Written for Beyond Linux From Scratch
|
---|
128 | # by James Robertson <jameswrobertson@earthlink.net>
|
---|
129 |
|
---|
130 | # System wide aliases and functions.
|
---|
131 |
|
---|
132 | # System wide environment variables and startup programs should go into
|
---|
133 | # /etc/profile. Personal environment variables and startup programs
|
---|
134 | # should go into ~/.bash_profile. Personal aliases and functions should
|
---|
135 | # go into ~/.bashrc
|
---|
136 |
|
---|
137 | # By default we want the umask to get set.
|
---|
138 | # Even for non-interactive and non-login shells.
|
---|
139 | if [ "$(id -gn)" = "$(id -un)" -a $(id -u) -gt 99 ] ; then
|
---|
140 | umask 002
|
---|
141 | else
|
---|
142 | umask 022
|
---|
143 | fi
|
---|
144 |
|
---|
145 | # Provides a colored /bin/ls command. Used in conjunction with code in
|
---|
146 | # /etc/profile.
|
---|
147 | alias ls='ls --color=auto'
|
---|
148 |
|
---|
149 | # Provides prompt for non-interactive shells, specifically shells started
|
---|
150 | # in the xfree environment. [Review archive thread titled PS1
|
---|
151 | # Environment Variable for a great case study behind this script
|
---|
152 | # addendum.]
|
---|
153 | export PS1="[\u@\h \w]\\$ "
|
---|
154 |
|
---|
155 | # End /etc/bashrc
|
---|
156 | <command>EOF</command></userinput></screen>
|
---|
157 |
|
---|
158 | <para>Here is a base <filename>~/.bash_profile</filename>. Comments in
|
---|
159 | the file should explain everything you need. If you want each new user
|
---|
160 | to have this file automatically provided, just change the output of the
|
---|
161 | next command to <filename>/etc/skel/.bash_profile</filename> and check the
|
---|
162 | permissions after the command is run. You can then copy
|
---|
163 | <filename>/etc/skel/.bash_profile</filename> to the home directories of
|
---|
164 | already existing users, including root, and set the owner and group
|
---|
165 | appropriately.
|
---|
166 | </para>
|
---|
167 |
|
---|
168 | <screen><userinput><command>cat > ~/.bash_profile << "EOF"</command>
|
---|
169 | # Begin ~/.bash_profile
|
---|
170 | # Written for Beyond Linux From Scratch
|
---|
171 | # by James Robertson <jameswrobertson@earthlink.net>
|
---|
172 |
|
---|
173 | # Personal environment variables and startup programs.
|
---|
174 |
|
---|
175 | # Personal aliases and functions should go in ~/.bashrc. System wide
|
---|
176 | # environment variables and startup programs are in /etc/profile.
|
---|
177 | # System wide aliases and functions are in /etc/bashrc.
|
---|
178 |
|
---|
179 | if [ -f "$HOME/.bashrc" ] ; then
|
---|
180 | source $HOME/.bashrc
|
---|
181 | fi
|
---|
182 |
|
---|
183 | if [ -d "$HOME/bin" ] ; then
|
---|
184 | pathman $HOME/bin last
|
---|
185 | fi
|
---|
186 |
|
---|
187 | export PATH
|
---|
188 |
|
---|
189 | # End ~/.bash_profile
|
---|
190 | <command>EOF</command></userinput></screen>
|
---|
191 |
|
---|
192 | <para>Here is a base <filename>~/.bashrc</filename>. Comments in the
|
---|
193 | file should explain everything you need. The comments and
|
---|
194 | instructions for using <filename>/etc/skel</filename> for
|
---|
195 | <filename>.bash_profile</filename> above also apply here. Only the
|
---|
196 | target file names are different.</para>
|
---|
197 |
|
---|
198 | <screen><userinput><command>cat > ~/.bashrc << "EOF"</command>
|
---|
199 | # Begin ~/.bashrc
|
---|
200 | # Written for Beyond Linux From Scratch
|
---|
201 | # by James Robertson <jameswrobertson@earthlink.net>
|
---|
202 |
|
---|
203 | # Personal aliases and functions.
|
---|
204 |
|
---|
205 | # Personal environment variables and startup programs should go in
|
---|
206 | # ~/.bash_profile. System wide environment variables and startup
|
---|
207 | # programs are in /etc/profile. System wide aliases and functions are
|
---|
208 | # in /etc/bashrc.
|
---|
209 |
|
---|
210 | if [ -f "/etc/bashrc" ] ; then
|
---|
211 | source /etc/bashrc
|
---|
212 | fi
|
---|
213 |
|
---|
214 | # End ~/.bashrc
|
---|
215 | <command>EOF</command></userinput></screen>
|
---|
216 |
|
---|
217 | <para>Here is a base <filename>~/.bash_logout</filename>. Comments in
|
---|
218 | the file should explain everything you need. You will notice that the
|
---|
219 | base <filename>~/.bash_logout</filename> does not include a
|
---|
220 | <userinput>clear</userinput> command. This is because the clear is
|
---|
221 | handled in the <filename>/etc/issue</filename> file.</para>
|
---|
222 |
|
---|
223 | <screen><userinput><command>cat > ~/.bash_logout << "EOF"</command>
|
---|
224 | # Begin ~/.bash_logout
|
---|
225 | # Written for Beyond Linux From Scratch
|
---|
226 | # by James Robertson <jameswrobertson@earthlink.net>
|
---|
227 |
|
---|
228 | # Personal items to perform on logout.
|
---|
229 |
|
---|
230 | # End ~/.bash_logout
|
---|
231 | <command>EOF</command></userinput></screen>
|
---|
232 |
|
---|
233 | <para>
|
---|
234 | If you want to use the <filename>dircolors</filename> capability
|
---|
235 | then run the following command. The <filename>/etc/skel</filename> setup
|
---|
236 | steps seen above also can be used here to provide a
|
---|
237 | <filename>.dircolors</filename> file when a new user is set up. As
|
---|
238 | before, just change the output file name on the following command and
|
---|
239 | assure the permissions, owner and group are correct on the files created
|
---|
240 | and/or copied.
|
---|
241 | </para>
|
---|
242 |
|
---|
243 | <para>
|
---|
244 | <userinput><command>/bin/dircolors -p > /etc/dircolors</command></userinput>
|
---|
245 | </para>
|
---|
246 |
|
---|
247 | <para>Ian Macdonald has written an excellent collection of tips and
|
---|
248 | tricks to enhance your shell environment. You can read it online at
|
---|
249 | <ulink
|
---|
250 | url="http://www.caliban.org/bash/index.shtml">http://www.caliban.org/bash/index.shtml</ulink></para>
|
---|
251 |
|
---|
252 | </sect1>
|
---|