Notes/demo/root/Trilium Demo/Tech/Linux/Bash scripting/Bash startup modes.html
2025-03-30 23:09:17 +03:00

203 lines
18 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="../../../../../style.css">
<base target="_parent">
<title data-trilium-title>Bash startup modes</title>
</head>
<body>
<div class="content">
<h1 data-trilium-h1>Bash startup modes</h1>
<div class="ck-content">
<div class="content">
<div class="ck-content">
<div class="content">
<div class="ck-content">
<div class="content">
<div class="ck-content">
<div class="content">
<div class="ck-content">
<div class="content">
<div class="ck-content">
<div class="content">
<div class="ck-content">
<div class="content">
<div class="ck-content">
<div class="content">
<div class="ck-content">
<div class="content">
<div class="ck-content">
<div class="content">
<div class="ck-content">
<div class="content">
<div class="ck-content">
<div class="content">
<div class="ck-content">
<div class="content">
<div class="ck-content">
<div class="content">
<div class="ck-content">
<div class="content">
<div class="ck-content">
<div class="content">
<div class="ck-content">
<div class="content">
<div class="ck-content">
<div class="content">
<div class="ck-content">
<div class="content">
<div class="ck-content">
<div class="content">
<div class="ck-content">
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<h3>Login shell</h3>
<p>As a "login shell", Bash reads and sets (executes) the user's profile
from /etc/profile and one of ~/.bash_profile, ~/.bash_login, or ~/.profile
(in that order, using the first one that's readable!).</p>
<p>When a login shell exits, Bash reads and executes commands from the file
~/.bash_logout, if it exists.</p>
<p>Why an extra login shell mode? There are many actions and variable sets
that only make sense for the initial user login. That's why all UNIX® shells
have (should have) a "login" mode.</p>
<p><i><strong>Methods to start Bash as a login shell:</strong></i>
</p>
<ul>
<li>the first character of argv[0] is - (a hyphen): traditional UNIX® shells
start from the login binary</li>
<li>Bash is started with the -l option</li>
<li>Bash is started with the --login option</li>
</ul>
<p><i><strong>Methods to test for login shell mode:</strong></i>
</p>
<ul>
<li>the shell option <a href="http://wiki.bash-hackers.org/internals/shell_options#login_shell">login_shell</a> is
set</li>
</ul>
<p><i><strong>Related switches:</strong></i>
</p>
<ul>
<li>--noprofile disables reading of all profile files</li>
</ul>
<h3>Interactive shell</h3>
<p>When Bash starts as an interactive non-login shell, it reads and executes
commands from ~/.bashrc. This file should contain, for example, aliases,
since they need to be defined in every shell as they're not inherited from
the parent shell.</p>
<p>The feature to have a system-wide /etc/bash.bashrc or a similar system-wide
rc-file is specific to vendors and distributors that ship <i>their own, patched variant of Bash</i>.
The classic way to have a system-wide rc file is to source /etc/bashrc
from every user's ~/.bashrc.</p>
<p><i><strong>Methods to test for interactive-shell mode:</strong></i>
</p>
<ul>
<li>the special parameter $- contains the letter i (lowercase I)</li>
</ul>
<p><i><strong>Related switches:</strong></i>
</p>
<ul>
<li>-i forces the interactive mode</li>
<li>--norc disables reading of the startup files (e.g. /etc/bash.bashrc if
supported) and ~/.bashrc</li>
<li>--rcfile defines another startup file (instead of /etc/bash.bashrc and
~/.bashrc)</li>
</ul>
<h3>SH mode</h3>
<p>When Bash starts in SH compatiblity mode, it tries to mimic the startup
behaviour of historical versions of sh as closely as possible, while conforming
to the POSIX® standard as well. The profile files read are /etc/profile
and ~/.profile, if it's a login shell.</p>
<p>If it's not a login shell, the environment variable <a href="http://wiki.bash-hackers.org/syntax/shellvars#env">ENV</a> is
evaluated and the resulting filename is used as the name of the startup
file.</p>
<p>After the startup files are read, Bash enters the <a href="http://wiki.bash-hackers.org/scripting/bashbehaviour#posix_run_mode">POSIX(r) compatiblity mode (for running, not for starting!)</a>.</p>
<p><i><strong>Bash starts in sh compatiblity mode when:</strong></i>
</p>
<ul>
<li>the base filename in argv[0] is sh (
<figure class="image">
<img />
</figure> NB: /bin/sh may be linked to /bin/bash, but that doesn't mean it acts
like /bin/bash 
<figure class="image">
<img />
</figure>)</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>