Normally bash knows that it's a login shell because when the login program invokes it, it tells bash that its name is -bash
. That name is in argv[0]
, the zeroth command line argument, which is conventionally the way the user invoked the program. The initial hyphen is a convention to tell a shell that it's a login shell. Bash also behaves as a login shell if you pass it the option --login
or -l
. See Difference between Login Shell and Non-Login Shell? for more details.
As of Jailkit 2.16, jk_chrootsh
reads the absolute path to the shell to invoke from various sources, and passes this path as argv[0]
, and passes its own command line arguments down to that shell. In the normal use case where jk_chrootsh
is itself used in /etc/passwd
, there is no way to pass an argument such as -l
. Since the absolute path doesn't begin with -
, there is no way to make jk_chrootsh
invoke a login shell, short of using a tiny intermediate program.
#include <unistd.h>
int main () {
execl("/bin/bash", "-bash", NULL);
return 127;
}
I would have expected jk_chrootsh
to have an easy way of invoking a login shell. I suggest making a feature request.
The way it's supposed work is that, at the point when you get a shell prompt, both .profile
and .bashrc
have been run. The specific details of how you get to that point are of secondary relevance, but if either of the files didn't get run at all, you'd have a shell with incomplete settings.
The reason terminal emulators on Linux (and other X-based systems) don't need to run .profile
themselves is that it will normally have been run already when you logged in to X. The settings in .profile
are supposed to be of the kind that can be inherited by subprocesses, so as long as it's executed once when you log in (e.g. via .Xsession
), any further subshells don't need to re-run it.
As the Debian wiki page linked by Alan Shutko explains:
"Why is .bashrc
a separate file from .bash_profile
, then? This is done for mostly historical reasons, when machines were extremely slow compared to today's workstations. Processing the commands in .profile
or .bash_profile
could take quite a long time, especially on a machine where a lot of the work had to be done by external commands (pre-bash). So the difficult initial set-up commands, which create environment variables that can be passed down to child processes, are put in .bash_profile
. The transient settings and aliases which are not inherited are put in .bashrc
so that they can be re-read by every subshell."
All the same rules hold on OSX, too, except for one thing — the OSX GUI doesn't run .profile
when you log in, apparently because it has its own method of loading global settings. But that means that a terminal emulator on OSX does need to run .profile
(by telling the shell it launches that it's a login shell), otherwise you'd end up with a potentially crippled shell.
Now, a kind of a silly peculiarity of bash, not shared by most other shells, is that it will not automatically run .bashrc
if it's started as a login shell. The standard work-around for that is to include something like the following commands in .bash_profile
:
[[ -e ~/.profile ]] && source ~/.profile # load generic profile settings
[[ -e ~/.bashrc ]] && source ~/.bashrc # load aliases etc.
Alternatively, it's possible to have no .bash_profile
at all, and just include some bash-specific code in the generic .profile
file to run .bashrc
if needed.
If the OSX default .bash_profile
or .profile
doesn't do this, then that's arguably a bug. In any case, the proper work-around is to simply add those lines to .bash_profile
.
Edit: As strugee notes, the default shell on OSX used to be tcsh, whose behavior is much saner in this respect: when run as an interactive login shell, tcsh automatically reads both .profile
and .tcshrc
/ .cshrc
, and thus does not need any workarounds like the .bash_profile
trick shown above.
Based on this, I'm 99% sure that the failure of OSX to supply an appropriate default .bash_profile
is because, when they switched from tcsh to bash, the folks at Apple simply didn't notice this little wart in bash's startup behavior. With tcsh, no such tricks were needed — starting tcsh as a login shell from an OSX terminal emulator Just Plain Works and does the right thing without such kluges.
Best Answer
No, you’re not correct. Bash behaves as documented:
--login
flag;--login
flag.A shell can be a login shell without the
--login
flag. If you look at/proc/$$/cmdline
from a Bash shell started by SSH on a Linux system, you’ll see it was started as-bash
— the leading hyphen is the usual way of starting a login shell, and isn’t covered by the first section if it ends up being non-interactive. However if one wanted a non-interactive login shell for whatever reason, one would typically use--login
to get it.