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