Session initialization files are weird, for hysterical historical reasons.
A long time ago, you logged in on the console, period. When you logged in, you got a shell, and the shell would load some initialization file. For Bourne-style shells, there are two initialization files: /etc/profile
(for all users) and ~/.profile
(for each user). The initialization file can set environment variables such as PATH
, load applications that you want to run in every session (e.g. a mail indicator), etc. If a shell isn't a login shell, it doesn't need to start anything or set environment variables, so there's no need to run any initialization file. Surely there isn't…
Actually, shells gradually gained interactive features, so there's a need for an initialization file for interactive shells. Ksh has ~/.kshrc
, bash has ~/.bashrc
, zsh has ~/.zshrc
.
Remote interactive logins run /etc/profile
and ~/.profile
. But these are not suitable to non-interactive logins because they may start non-interactive programs. Unfortunately, no standard has emerged for an initialization file for non-interactive logins.
You can run /etc/profile
and ~/.profile
manually if you're sure they won't produce any output or start any program, just set environment variables.
ssh foo '. /etc/profile; . ~/.profile; somecommand'
If you have parts in your .profile
that only make sense in an interactive session, you can put them inside a conditional instruction.
## (in ~/.profile)
case $- in
*i*) # the shell is interactive
newmail;;
esac
If your login shell is bash, you can take advantage of a quirk: the initialization file ~/.bashrc
is read by interactive shells, and also by non-interactive shells whose parent process is called rshd
or sshd
. Other shells don't have this quirk.
## (in ~/.bashrc)
case $- in
*i*) # interactive shell: set prompt, completion settings, key bindings, etc.
…;;
*) # non-interactive shell: our parent must be rshd or sshd
. ~/.profile;;
esac