[f45b1953] | 1 | <sect1 id="postlfs-config-profile">
|
---|
| 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
|
---|
| 8 | may affect login and interactive environments differently.</para>
|
---|
[f45b1953] | 9 |
|
---|
[b554263] | 10 | <para>An interactive login shell is started after a successful login by
|
---|
[036393e1] | 11 | <filename>/bin/login</filename> by reading the
|
---|
| 12 | <filename>/etc/passwd</filename> file. An
|
---|
[b554263] | 13 | interactive non-login shell is started at the command line (e.g.
|
---|
[766bbe40] | 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
|
---|
[b554263] | 16 | because it is processing a script and not waiting for user input between
|
---|
| 17 | commands.</para>
|
---|
| 18 |
|
---|
[766bbe40] | 19 | <para>For more information see <command>info bash</command> --
|
---|
| 20 | <emphasis role="strong">Nodes: Bash Startup Files and Interactive
|
---|
| 21 | Shells.</emphasis></para>
|
---|
[b554263] | 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
|
---|
[64d97b7c] | 28 | <filename>~/.bash_logout</filename> is not used for an invocation of the
|
---|
[b554263] | 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 |
|
---|
[7008de1] | 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
|
---|
[766bbe40] | 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>
|
---|
[b554263] | 42 |
|
---|
[766bbe40] | 43 | <screen><userinput><command>cat > /etc/profile << "EOF"</command>
|
---|
| 44 | # Begin /etc/profile
|
---|
[b554263] | 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 |
|
---|
[766bbe40] | 103 | # End /etc/profile
|
---|
| 104 | <command>EOF</command></userinput></screen>
|
---|
[b554263] | 105 |
|
---|
| 106 | <para>Here is a base <filename>/etc/bashrc</filename>. Comments in the
|
---|
| 107 | file should explain everything you need.</para>
|
---|
| 108 |
|
---|
[766bbe40] | 109 | <screen><userinput><command>cat > /etc/bashrc << "EOF"</command>
|
---|
| 110 | # Begin /etc/bashrc
|
---|
[b554263] | 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
|
---|
[f45b1953] | 128 |
|
---|
[b554263] | 129 | # Provides a colored /bin/ls command. Used in conjunction with code in
|
---|
| 130 | # /etc/profile.
|
---|
| 131 | alias ls='ls --color=auto'
|
---|
| 132 |
|
---|
[28a9992] | 133 | # Provides prompt for non-interactive shells, specifically shells started
|
---|
| 134 | # in the xfree environment. [Review archive thread titled PS1
|
---|
| 135 | # Environment Variable for a great case study behind this script
|
---|
| 136 | # addendum.]
|
---|
| 137 | export PS1="[\u@\h \w]\\$ "
|
---|
| 138 |
|
---|
[766bbe40] | 139 | # End /etc/bashrc
|
---|
| 140 | <command>EOF</command></userinput></screen>
|
---|
[b554263] | 141 |
|
---|
| 142 | <para>Here is a base <filename>~/.bash_profile</filename>. Comments in
|
---|
| 143 | the file should explain everything you need.</para>
|
---|
| 144 |
|
---|
[766bbe40] | 145 | <screen><userinput><command>cat > ~/.bash_profile << "EOF"</command>
|
---|
| 146 | # Begin ~/.bash_profile
|
---|
[b554263] | 147 | # Written for Beyond Linux From Scratch
|
---|
| 148 | # by James Robertson <jameswrobertson@earthlink.net>
|
---|
| 149 |
|
---|
[64d97b7c] | 150 | # Personal environment variables and startup programs.
|
---|
[b554263] | 151 |
|
---|
| 152 | # Personal aliases and functions should go in ~/.bashrc. System wide
|
---|
| 153 | # environment variables and startup programs are in /etc/profile.
|
---|
| 154 | # System wide aliases and functions are in /etc/bashrc.
|
---|
| 155 |
|
---|
| 156 | if [ -f "$HOME/.bashrc" ] ; then
|
---|
[7008de1] | 157 | source $HOME/.bashrc
|
---|
[b554263] | 158 | fi
|
---|
| 159 |
|
---|
| 160 | if [ -d "$HOME/bin" ] ; then
|
---|
| 161 | pathman $HOME/bin last
|
---|
| 162 | fi
|
---|
| 163 |
|
---|
| 164 | export PATH
|
---|
| 165 |
|
---|
[766bbe40] | 166 | # End ~/.bash_profile
|
---|
| 167 | <command>EOF</command></userinput></screen>
|
---|
[b554263] | 168 |
|
---|
| 169 | <para>Here is a base <filename>~/.bashrc</filename>. Comments in the
|
---|
| 170 | file should explain everything you need.</para>
|
---|
| 171 |
|
---|
[766bbe40] | 172 | <screen><userinput><command>cat > ~/.bashrc << "EOF"</command>
|
---|
| 173 | # Begin ~/.bashrc
|
---|
[b554263] | 174 | # Written for Beyond Linux From Scratch
|
---|
| 175 | # by James Robertson <jameswrobertson@earthlink.net>
|
---|
| 176 |
|
---|
| 177 | # Personal aliases and functions.
|
---|
| 178 |
|
---|
| 179 | # Personal environment variables and startup programs should go in
|
---|
[64d97b7c] | 180 | # ~/.bash_profile. System wide environment variables and startup
|
---|
[b554263] | 181 | # programs are in /etc/profile. System wide aliases and functions are
|
---|
| 182 | # in /etc/bashrc.
|
---|
| 183 |
|
---|
| 184 | if [ -f "/etc/bashrc" ] ; then
|
---|
[7008de1] | 185 | source /etc/bashrc
|
---|
[b554263] | 186 | fi
|
---|
| 187 |
|
---|
[766bbe40] | 188 | # End ~/.bashrc
|
---|
| 189 | <command>EOF</command></userinput></screen>
|
---|
[b554263] | 190 |
|
---|
| 191 | <para>Here is a base <filename>~/.bash_logout</filename>. Comments in
|
---|
[7008de1] | 192 | the file should explain everything you need. You will notice that the
|
---|
| 193 | base <filename>~/.bash_logout</filename> does not include a
|
---|
| 194 | <userinput>clear</userinput> command. This is because the clear is
|
---|
| 195 | handled in the <filename>/etc/issue</filename> file.</para>
|
---|
[b554263] | 196 |
|
---|
[766bbe40] | 197 | <screen><userinput><command>cat > ~/.bash_logout << "EOF"</command>
|
---|
| 198 | # Begin ~/.bash_logout
|
---|
[b554263] | 199 | # Written for Beyond Linux From Scratch
|
---|
| 200 | # by James Robertson <jameswrobertson@earthlink.net>
|
---|
| 201 |
|
---|
| 202 | # Personal items to perform on logout.
|
---|
| 203 |
|
---|
[766bbe40] | 204 | # End ~/.bash_logout
|
---|
| 205 | <command>EOF</command></userinput></screen>
|
---|
[b554263] | 206 |
|
---|
| 207 | <para>If you want to use the <filename>/etc/dircolors</filename> or
|
---|
| 208 | <filename>~/.dircolors</filename> files called from
|
---|
| 209 | <filename>/etc/profile</filename>, then run the following:
|
---|
| 210 | <userinput>/bin/dircolors -p > /etc/dircolors</userinput> or
|
---|
| 211 | <userinput>/bin/dircolors -p > ~/.dircolors</userinput> respectively.
|
---|
| 212 | The file in the <filename>/etc</filename> directory should be used for
|
---|
| 213 | global settings and if one exists in your home directory then it will
|
---|
| 214 | overwrite the global settings. It might be a good idea to create a base
|
---|
| 215 | <filename>.dircolors</filename> file and place it in the
|
---|
| 216 | <filename>/etc/skel</filename> directory for new users.</para>
|
---|
| 217 |
|
---|
[7008de1] | 218 | <para>Ian Macdonald has written an excellent collection of tips and
|
---|
| 219 | tricks to enhance your shell environment. You can read it online at
|
---|
| 220 | <ulink
|
---|
| 221 | url="http://www.caliban.org/bash/index.shtml">http://www.caliban.org/bash/index.shtml</ulink></para>
|
---|
| 222 |
|
---|
[b554263] | 223 | </sect1>
|
---|