Unix shell initialization
Shell initialization files are the way to maintain a unified configuration for parameters such as:
$PATHand other environment variables- shell prompt
- shell tab-completion
- aliases, functions
- key bindings
Shell Modes #
Which files are loaded and executed upon shell startup depends on which mode is used, or simply put, how the shell was launched. There are two main variants:
- login - i.e., when a user has logged into the system without initializing a graphical shell, or by using the SSH protocol;
- interactive - a shell that has a prompt and whose standard input and output are connected to a terminal.
These modes can be manually activated using the following flags:
l,-logini
As a result, the following common operations will be executed as follows:
- log in to a remote system via SSH: login + interactive
- execute a script remotely, e.g.
ssh user@host 'echo $PWD'or with Capistrano: non‑login, non‑interactive - execute a script remotely and request a terminal, e.g.
ssh user@host -t 'echo $PWD': non-login, interactive - start a new shell process, e.g.
bash: non‑login, interactive - run a script,
bash myscript.sh: non‑login, non‑interactive - run an executable with
#!/usr/bin/env bashshebang: non‑login, non‑interactive - open a new graphical terminal window/tab:
- on Mac OS X: login, interactive
- on Linux: non‑login, interactive
Shell init files #
In order of activation:
bash #
- login mode:
/etc/profile~/.bash_profile,~/.bash_login,~/.profile(only first one that exists)
- interactive non-login:
/etc/bash.bashrc(some Linux; not on Mac OS X)~/.bashrc
- non-interactive:
- source file in
$BASH_ENV
- source file in
Zsh #
/etc/zshenv~/.zshenv- login mode:
/etc/zprofile~/.zprofile
- interactive:
/etc/zshrc~/.zshrc
- login mode:
/etc/zlogin~/.zlogin
dash #
- login mode:
/etc/profile~/.profile
- interactive:
- source file in
$ENV
- source file in
fish #
<install-prefix>/config.fish/etc/fish/config.fish~/.config/fish/config.fish
Practical guide to which files get sourced when #
- Opening a new Terminal window/tab:
- bash
- OS X:
.bash_profileor.profile(1st found) - Linux:
.profile(Ubuntu, once per desktop login session) +.bashrc
- OS X:
- Zsh
- OS X:
.zshenv+.zprofile+.zshrc - Linux:
.profile(Ubuntu, once per desktop login session) +.zshenv+.zshrc
- OS X:
- bash
- Logging into a system via SSH:
- bash:
.bash_profileor.profile(1st found) - Zsh:
.zshenv+.zprofile+.zshrc
- bash:
- Executing a command remotely with
sshor Capistrano:- bash:
.bashrc - Zsh:
.zshenv
- bash:
- Remote git hook triggered by push over SSH:
- no init files get sourced, since hooks are running within a restricted shell
- PATH will be roughly:
/usr/libexec/git-core:/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin
Misc. things that affect $PATH #
- OS X:
/etc/paths,/etc/paths.d/*~/.MacOSX/environment.plist- affects all graphical programs/etc/launchd.conf- TextMate: Preferences -> Advanced -> Shell Variables
- Linux:
/etc/environment
Final notes #
This guide was tested with:
- bash 4.2.37, 4.2.39
- Zsh 4.3.11, 5.0
On these operating systems/apps:
- Mac OS X 10.8 (Mountain Lion): Terminal.app, iTerm2
- Ubuntu 12.10: Terminal
See also:
path_helper(8)
launchd.conf(5)
pam_env(8)