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