summaryrefslogtreecommitdiff
path: root/devdocs/bash/bash-startup-files.html
blob: c24181b037530b97e6a1a36616da8d51f3477c67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<h1 class="section">Bash Startup Files</h1>  <p>This section describes how Bash executes its startup files. If any of the files exist but cannot be read, Bash reports an error. Tildes are expanded in filenames as described above under Tilde Expansion (see <a href="tilde-expansion">Tilde Expansion</a>). </p> <p>Interactive shells are described in <a href="interactive-shells">Interactive Shells</a>. </p> <h4 class="subsubheading">Invoked as an interactive login shell, or with <samp>--login</samp>
</h4> <p>When Bash is invoked as an interactive login shell, or as a non-interactive shell with the <samp>--login</samp> option, it first reads and executes commands from the file <samp>/etc/profile</samp>, if that file exists. After reading that file, it looks for <samp>~/.bash_profile</samp>, <samp>~/.bash_login</samp>, and <samp>~/.profile</samp>, in that order, and reads and executes commands from the first one that exists and is readable. The <samp>--noprofile</samp> option may be used when the shell is started to inhibit this behavior. </p> <p>When an interactive login shell exits, or a non-interactive login shell executes the <code>exit</code> builtin command, Bash reads and executes commands from the file <samp>~/.bash_logout</samp>, if it exists. </p> <h4 class="subsubheading">Invoked as an interactive non-login shell</h4> <p>When an interactive shell that is not a login shell is started, Bash reads and executes commands from <samp>~/.bashrc</samp>, if that file exists. This may be inhibited by using the <samp>--norc</samp> option. The <samp>--rcfile <var>file</var></samp> option will force Bash to read and execute commands from <var>file</var> instead of <samp>~/.bashrc</samp>. </p> <p>So, typically, your <samp>~/.bash_profile</samp> contains the line </p>
<div class="example"> <pre class="example">if [ -f ~/.bashrc ]; then . ~/.bashrc; fi
</pre>
</div> <p>after (or before) any login-specific initializations. </p> <h4 class="subsubheading">Invoked non-interactively</h4> <p>When Bash is started non-interactively, to run a shell script, for example, it looks for the variable <code>BASH_ENV</code> in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute. Bash behaves as if the following command were executed: </p>
<div class="example"> <pre class="example">if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
</pre>
</div> <p>but the value of the <code>PATH</code> variable is not used to search for the filename. </p> <p>As noted above, if a non-interactive shell is invoked with the <samp>--login</samp> option, Bash attempts to read and execute commands from the login shell startup files. </p> <h4 class="subsubheading">Invoked with name <code>sh</code>
</h4> <p>If Bash is invoked with the name <code>sh</code>, it tries to mimic the startup behavior of historical versions of <code>sh</code> as closely as possible, while conforming to the <small>POSIX</small> standard as well. </p> <p>When invoked as an interactive login shell, or as a non-interactive shell with the <samp>--login</samp> option, it first attempts to read and execute commands from <samp>/etc/profile</samp> and <samp>~/.profile</samp>, in that order. The <samp>--noprofile</samp> option may be used to inhibit this behavior. When invoked as an interactive shell with the name <code>sh</code>, Bash looks for the variable <code>ENV</code>, expands its value if it is defined, and uses the expanded value as the name of a file to read and execute. Since a shell invoked as <code>sh</code> does not attempt to read and execute commands from any other startup files, the <samp>--rcfile</samp> option has no effect. A non-interactive shell invoked with the name <code>sh</code> does not attempt to read any other startup files. </p> <p>When invoked as <code>sh</code>, Bash enters <small>POSIX</small> mode after the startup files are read. </p> <h4 class="subsubheading">Invoked in <small>POSIX</small> mode</h4> <p>When Bash is started in <small>POSIX</small> mode, as with the <samp>--posix</samp> command line option, it follows the <small>POSIX</small> standard for startup files. In this mode, interactive shells expand the <code>ENV</code> variable and commands are read and executed from the file whose name is the expanded value. No other startup files are read. </p> <h4 class="subsubheading">Invoked by remote shell daemon</h4> <p>Bash attempts to determine when it is being run with its standard input connected to a network connection, as when executed by the historical remote shell daemon, usually <code>rshd</code>, or the secure shell daemon <code>sshd</code>. If Bash determines it is being run non-interactively in this fashion, it reads and executes commands from <samp>~/.bashrc</samp>, if that file exists and is readable. It will not do this if invoked as <code>sh</code>. The <samp>--norc</samp> option may be used to inhibit this behavior, and the <samp>--rcfile</samp> option may be used to force another file to be read, but neither <code>rshd</code> nor <code>sshd</code> generally invoke the shell with those options or allow them to be specified. </p> <h4 class="subsubheading">Invoked with unequal effective and real <small>UID/GID</small>s</h4> <p>If Bash is started with the effective user (group) id not equal to the real user (group) id, and the <samp>-p</samp> option is not supplied, no startup files are read, shell functions are not inherited from the environment, the <code>SHELLOPTS</code>, <code>BASHOPTS</code>, <code>CDPATH</code>, and <code>GLOBIGNORE</code> variables, if they appear in the environment, are ignored, and the effective user id is set to the real user id. If the <samp>-p</samp> option is supplied at invocation, the startup behavior is the same, but the effective user id is not reset. </p><div class="_attribution">
  <p class="_attribution-p">
    Copyright &copy; 2000, 2001, 2002, 2007, 2008 Free Software Foundation, Inc.<br>Licensed under the GNU Free Documentation License.<br>
    <a href="https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html" class="_attribution-link">https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html</a>
  </p>
</div>