[78b3cd61] | 1 | <sect1 id="postlfs-config-profile" xreflabel="The Bash Shell Startup Files">
|
---|
[f45b1953] | 2 | <?dbhtml filename="profile.html" dir="postlfs"?>
|
---|
[b554263] | 3 | <title>The Bash Shell Startup Files</title>
|
---|
[f45b1953] | 4 |
|
---|
[b554263] | 5 | <para>The shell program <filename>/bin/bash</filename> (hereafter
|
---|
[64d97b7c] | 6 | referred to as just "the shell") uses a collection of startup files to
|
---|
[b554263] | 7 | help create an environment to run in. Each file has a specific use and
|
---|
[cfc2a54] | 8 | may affect login and interactive environments differently. The files in
|
---|
[8604d92f] | 9 | the <filename class="directory">/etc</filename> directory generally provide global
|
---|
[cfc2a54] | 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
|
---|
[036393e1] | 16 | <filename>/etc/passwd</filename> file. An
|
---|
[b7d0bb4] | 17 | interactive non-login shell is started at the command-line (e.g.
|
---|
[766bbe40] | 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
|
---|
[b554263] | 20 | because it is processing a script and not waiting for user input between
|
---|
| 21 | commands.</para>
|
---|
| 22 |
|
---|
[766bbe40] | 23 | <para>For more information see <command>info bash</command> --
|
---|
| 24 | <emphasis role="strong">Nodes: Bash Startup Files and Interactive
|
---|
| 25 | Shells.</emphasis></para>
|
---|
[b554263] | 26 |
|
---|
[cfc2a54] | 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
|
---|
[b554263] | 30 | <filename>~/.bash_profile</filename>, and
|
---|
[cfc2a54] | 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
|
---|
[b554263] | 41 | <filename>~/.bash_profile</filename> are read when the shell is invoked
|
---|
[cfc2a54] | 42 | as an interactive login shell. The file <filename>~/.bashrc</filename>
|
---|
[b554263] | 43 | is read when the shell is invoked as an interactive non-login
|
---|
[cfc2a54] | 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
|
---|
[3df86b66] | 49 | things like <command>ls --color</command>.
|
---|
[cfc2a54] | 50 | </para>
|
---|
[b554263] | 51 |
|
---|
[7008de1] | 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
|
---|
[766bbe40] | 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>
|
---|
[b554263] | 58 |
|
---|
[766bbe40] | 59 | <screen><userinput><command>cat > /etc/profile << "EOF"</command>
|
---|
| 60 | # Begin /etc/profile
|
---|
[b554263] | 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
|
---|
[30f1425] | 101 | HISTIGNORE="&:[bf]g:exit"
|
---|
[b554263] | 102 | PS1="[\u@\h \w]\\$ "
|
---|
| 103 |
|
---|
| 104 | # Setup the INPUTRC environment variable.
|
---|
| 105 | if [ -z "$INPUTRC" -a ! -f "$HOME/.inputrc" ] ; then
|
---|
| 106 | INPUTRC=/etc/inputrc
|
---|
| 107 | fi
|
---|
| 108 |
|
---|
| 109 | # Setup for /bin/ls to support color, the alias is in /etc/bashrc.
|
---|
| 110 | if [ -f "/etc/dircolors" ] ; then
|
---|
| 111 | eval $(dircolors -b /etc/dircolors)
|
---|
| 112 |
|
---|
| 113 | if [ -f "$HOME/.dircolors" ] ; then
|
---|
| 114 | eval $(dircolors -b $HOME/.dircolors)
|
---|
| 115 | fi
|
---|
| 116 | fi
|
---|
| 117 |
|
---|
[30f1425] | 118 | export PATH HISTSIZE HISTIGNORE PS1 LS_COLORS INPUTRC
|
---|
[b554263] | 119 |
|
---|
[766bbe40] | 120 | # End /etc/profile
|
---|
| 121 | <command>EOF</command></userinput></screen>
|
---|
[b554263] | 122 |
|
---|
| 123 | <para>Here is a base <filename>/etc/bashrc</filename>. Comments in the
|
---|
| 124 | file should explain everything you need.</para>
|
---|
| 125 |
|
---|
[766bbe40] | 126 | <screen><userinput><command>cat > /etc/bashrc << "EOF"</command>
|
---|
| 127 | # Begin /etc/bashrc
|
---|
[b554263] | 128 | # Written for Beyond Linux From Scratch
|
---|
| 129 | # by James Robertson <jameswrobertson@earthlink.net>
|
---|
| 130 |
|
---|
| 131 | # System wide aliases and functions.
|
---|
| 132 |
|
---|
| 133 | # System wide environment variables and startup programs should go into
|
---|
| 134 | # /etc/profile. Personal environment variables and startup programs
|
---|
| 135 | # should go into ~/.bash_profile. Personal aliases and functions should
|
---|
| 136 | # go into ~/.bashrc
|
---|
| 137 |
|
---|
| 138 | # By default we want the umask to get set.
|
---|
| 139 | # Even for non-interactive and non-login shells.
|
---|
| 140 | if [ "$(id -gn)" = "$(id -un)" -a $(id -u) -gt 99 ] ; then
|
---|
| 141 | umask 002
|
---|
| 142 | else
|
---|
| 143 | umask 022
|
---|
| 144 | fi
|
---|
[f45b1953] | 145 |
|
---|
[b554263] | 146 | # Provides a colored /bin/ls command. Used in conjunction with code in
|
---|
| 147 | # /etc/profile.
|
---|
| 148 | alias ls='ls --color=auto'
|
---|
| 149 |
|
---|
[28a9992] | 150 | # Provides prompt for non-interactive shells, specifically shells started
|
---|
| 151 | # in the xfree environment. [Review archive thread titled PS1
|
---|
| 152 | # Environment Variable for a great case study behind this script
|
---|
| 153 | # addendum.]
|
---|
| 154 | export PS1="[\u@\h \w]\\$ "
|
---|
| 155 |
|
---|
[766bbe40] | 156 | # End /etc/bashrc
|
---|
| 157 | <command>EOF</command></userinput></screen>
|
---|
[b554263] | 158 |
|
---|
| 159 | <para>Here is a base <filename>~/.bash_profile</filename>. Comments in
|
---|
[cfc2a54] | 160 | the file should explain everything you need. If you want each new user
|
---|
| 161 | to have this file automatically provided, just change the output of the
|
---|
| 162 | next command to <filename>/etc/skel/.bash_profile</filename> and check the
|
---|
| 163 | permissions after the command is run. You can then copy
|
---|
| 164 | <filename>/etc/skel/.bash_profile</filename> to the home directories of
|
---|
| 165 | already existing users, including root, and set the owner and group
|
---|
| 166 | appropriately.
|
---|
| 167 | </para>
|
---|
[b554263] | 168 |
|
---|
[766bbe40] | 169 | <screen><userinput><command>cat > ~/.bash_profile << "EOF"</command>
|
---|
| 170 | # Begin ~/.bash_profile
|
---|
[b554263] | 171 | # Written for Beyond Linux From Scratch
|
---|
| 172 | # by James Robertson <jameswrobertson@earthlink.net>
|
---|
| 173 |
|
---|
[64d97b7c] | 174 | # Personal environment variables and startup programs.
|
---|
[b554263] | 175 |
|
---|
| 176 | # Personal aliases and functions should go in ~/.bashrc. System wide
|
---|
| 177 | # environment variables and startup programs are in /etc/profile.
|
---|
| 178 | # System wide aliases and functions are in /etc/bashrc.
|
---|
| 179 |
|
---|
| 180 | if [ -f "$HOME/.bashrc" ] ; then
|
---|
[7008de1] | 181 | source $HOME/.bashrc
|
---|
[b554263] | 182 | fi
|
---|
| 183 |
|
---|
| 184 | if [ -d "$HOME/bin" ] ; then
|
---|
| 185 | pathman $HOME/bin last
|
---|
| 186 | fi
|
---|
| 187 |
|
---|
| 188 | export PATH
|
---|
| 189 |
|
---|
[766bbe40] | 190 | # End ~/.bash_profile
|
---|
| 191 | <command>EOF</command></userinput></screen>
|
---|
[b554263] | 192 |
|
---|
| 193 | <para>Here is a base <filename>~/.bashrc</filename>. Comments in the
|
---|
[cfc2a54] | 194 | file should explain everything you need. The comments and
|
---|
[8604d92f] | 195 | instructions for using <filename class="directory">/etc/skel</filename> for
|
---|
[cfc2a54] | 196 | <filename>.bash_profile</filename> above also apply here. Only the
|
---|
| 197 | target file names are different.</para>
|
---|
[b554263] | 198 |
|
---|
[766bbe40] | 199 | <screen><userinput><command>cat > ~/.bashrc << "EOF"</command>
|
---|
| 200 | # Begin ~/.bashrc
|
---|
[b554263] | 201 | # Written for Beyond Linux From Scratch
|
---|
| 202 | # by James Robertson <jameswrobertson@earthlink.net>
|
---|
| 203 |
|
---|
| 204 | # Personal aliases and functions.
|
---|
| 205 |
|
---|
| 206 | # Personal environment variables and startup programs should go in
|
---|
[64d97b7c] | 207 | # ~/.bash_profile. System wide environment variables and startup
|
---|
[b554263] | 208 | # programs are in /etc/profile. System wide aliases and functions are
|
---|
| 209 | # in /etc/bashrc.
|
---|
| 210 |
|
---|
| 211 | if [ -f "/etc/bashrc" ] ; then
|
---|
[7008de1] | 212 | source /etc/bashrc
|
---|
[b554263] | 213 | fi
|
---|
| 214 |
|
---|
[766bbe40] | 215 | # End ~/.bashrc
|
---|
| 216 | <command>EOF</command></userinput></screen>
|
---|
[b554263] | 217 |
|
---|
| 218 | <para>Here is a base <filename>~/.bash_logout</filename>. Comments in
|
---|
[7008de1] | 219 | the file should explain everything you need. You will notice that the
|
---|
| 220 | base <filename>~/.bash_logout</filename> does not include a
|
---|
| 221 | <userinput>clear</userinput> command. This is because the clear is
|
---|
| 222 | handled in the <filename>/etc/issue</filename> file.</para>
|
---|
[b554263] | 223 |
|
---|
[766bbe40] | 224 | <screen><userinput><command>cat > ~/.bash_logout << "EOF"</command>
|
---|
| 225 | # Begin ~/.bash_logout
|
---|
[b554263] | 226 | # Written for Beyond Linux From Scratch
|
---|
| 227 | # by James Robertson <jameswrobertson@earthlink.net>
|
---|
| 228 |
|
---|
| 229 | # Personal items to perform on logout.
|
---|
| 230 |
|
---|
[766bbe40] | 231 | # End ~/.bash_logout
|
---|
| 232 | <command>EOF</command></userinput></screen>
|
---|
[b554263] | 233 |
|
---|
[cfc2a54] | 234 | <para>
|
---|
| 235 | If you want to use the <filename>dircolors</filename> capability
|
---|
[8604d92f] | 236 | then run the following command. The <filename class="directory">/etc/skel</filename> setup
|
---|
[cfc2a54] | 237 | steps seen above also can be used here to provide a
|
---|
| 238 | <filename>.dircolors</filename> file when a new user is set up. As
|
---|
| 239 | before, just change the output file name on the following command and
|
---|
| 240 | assure the permissions, owner and group are correct on the files created
|
---|
| 241 | and/or copied.
|
---|
| 242 | </para>
|
---|
| 243 |
|
---|
| 244 | <para>
|
---|
| 245 | <userinput><command>/bin/dircolors -p > /etc/dircolors</command></userinput>
|
---|
| 246 | </para>
|
---|
[b554263] | 247 |
|
---|
[7008de1] | 248 | <para>Ian Macdonald has written an excellent collection of tips and
|
---|
| 249 | tricks to enhance your shell environment. You can read it online at
|
---|
| 250 | <ulink
|
---|
| 251 | url="http://www.caliban.org/bash/index.shtml">http://www.caliban.org/bash/index.shtml</ulink></para>
|
---|
| 252 |
|
---|
[b554263] | 253 | </sect1>
|
---|