Shell invocation
Languages: French
May 11, 2014
Bash and Zsh shells have very different ways to start, and this can be an issue when we switch from one to the other and back.
First, we distinguish two states
for the shell:
- Login/non-login shell,
- interactive/non-interactive shell.
Login shell
To indicate to a shell that it is a login shell, the calling process will
simply add a -
in front of his name (thus the shell will be called
-bash or -zsh). Some shells (like bash) allow command
line argument to fake login shells.
In a script, we can tell wether we are a login shell or not in a simple way:
-
Bash:
shopt -q login_shell -
Zsh:
[[ -o login ]]
Interactive shell
A shell is interactive if it is invoked without argument (or at least none that imply a command).
In a script, we can tell wether we are an interactive shell:
[[ $- == *i* ]]
(It is possible to have an interactive shell with a script, for instance if a file is sourced).
Zsh behavior when invoked
- In all cases,
/etc/zshenvand~/.zshenvare sourced (in this order). - If it is a login shell,
/etc/zprofileand~/.zprofileare sourced (in this order)Note
/etc/zprofileis chosen at compile-time and can vary depending on the system (On Archlinux it is/etc/zsh/zprofile). - If we have an interactive shell,
/etc/zshrcand~/.zshrcare sourced (in this order). - Finaly, if it is a login shell (again!),
/etc/zloginet~/.zloginare sourced (in this order).
Bash behavior when invoked
- If it is a login and interactive shell,
/etc/profileis sourced, and then the first existing one in~/.bash_profile,~/.bash_login,~/.profile. - If it is an interactive non-login shell,
~/.bashrcis sourced. - Finaly, if the shell is non-interactive,
$BASH_ENVis sourced (if available).
Bash behavior when invoked as sh
In many distributions, sh is simply a soft link to
bash. In that case, bash will behave in a different way when
invoked:
- If it is an interactive login shell,
/etc/profileand~/.profileare sourced. - If
$ENVis a file, it is also sourced.