2025-03-30 22:29:29 +03:00
< 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" >
2025-03-30 23:51:46 +03:00
< h3 > Login shell< / h3 >
2025-06-05 17:16:37 +02:00
2025-03-30 23:51:46 +03:00
< 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 > < em > < strong > Methods to start Bash as a login shell:< / strong > < / em >
2025-06-05 17:16:37 +02:00
2025-03-30 23:51:46 +03:00
< / 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 > < em > < strong > Methods to test for login shell mode:< / strong > < / em >
2025-06-05 17:16:37 +02:00
2025-03-30 23:51:46 +03:00
< / 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 > < em > < strong > Related switches:< / strong > < / em >
2025-06-05 17:16:37 +02:00
2025-03-30 23:51:46 +03:00
< / p >
< ul >
< li > --noprofile disables reading of all profile files< / li >
< / ul >
2025-03-31 22:55:55 +03:00
< h3 > Interactive shell< / h3 >
2025-06-05 17:16:37 +02:00
2025-03-30 23:51:46 +03:00
< 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 < em > their own, patched variant of Bash< / em > .
The classic way to have a system-wide rc file is to source /etc/bashrc
from every user's ~/.bashrc.< / p >
< p > < em > < strong > Methods to test for interactive-shell mode:< / strong > < / em >
2025-06-05 17:16:37 +02:00
2025-03-30 23:51:46 +03:00
< / p >
< ul >
< li > the special parameter $- contains the letter i (lowercase I)< / li >
< / ul >
< p > < em > < strong > Related switches:< / strong > < / em >
2025-06-05 17:16:37 +02:00
2025-03-30 23:51:46 +03:00
< / 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 >
2025-03-31 22:55:55 +03:00
< h3 > SH mode< / h3 >
2025-06-05 17:16:37 +02:00
2025-03-30 23:51:46 +03:00
< 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 > < em > < strong > Bash starts in sh compatiblity mode when:< / strong > < / em >
2025-06-05 17:16:37 +02:00
2025-03-30 23:51:46 +03:00
< / p >
< ul >
< li >
< p > the base filename in argv[0] is sh (< / p >
< figure class = "image" >
< img >
< / figure >
< p > NB: /bin/sh may be linked to /bin/bash, but that doesn't mean it
acts like /bin/bash < / p >
< figure class = "image" >
< img >
< / figure >
< p > )< / p >
< / li >
< / ul >
2025-03-30 22:29:29 +03:00
< / div >
< / div >
< / body >
< / html >