(this is probably a overkill, but maybe it'll be useful)
Some things to keep in mind:
Environment variables are somewhat public, and can be seen by other processes as easily as added an option to the ps(1)
command (like ps e $$
in bash) or looking at /proc/*/environ
, though both are restricted at least to the same user (or root) on modern systems. Don't rely on them being secret if you have another fairly easy option available.
~/.bashrc
is the wrong place for environment variables, since they can be computed once at login in ~/.bash_login
, ~/.bash_profile, or ~/.profile
, depending on your usage, and passed down to all descendent shells. In contrast, ~/.bashrc
actions tend to be recomputed on every shell invocation (unless explicitly disabled).
Putting bash code in the ~/.profile
can confuse other sh-descendent shells and non-shell tools which try to read that file, so having the bash-specific ~/.bash_login
or -_profile contain the bash-specific things, and using . ~/.profile
for the more general things (LESS, EDITOR, VISUAL, LC_COLLATE, LS_COLORS, etc), is friendlier to the other tools.
Environment variables in ~/.profile
should be in the old Bourne shell form (VAR=value ; export VAR
). On Linux, this isn't usually critical, though on other Unixen this can be a big issue when an older version of "sh" tries to read them.
Some X sessions will only read ~/.profile
, not ~/.bash_login
or the others mentioned above. Some will look for a ~/.xsession
file will need to be modified to have . $HOME/.profile
if it doesn't already somehow.
System-wide settings would be put instead in something like /etc/profile.d/similar-to-heroku.sh
. Note that the ".sh" is only present since the file will be used with "." or "source" - shell scripts should never have command-name extensions in any form of Unix/Linux.
Most environment variables get ditched when one sudo
s to root, as ybakos points out. Similar issues show up in crontabs, at jobs, etc. When in doubt, adding env | sort > /tmp/envvars
or the like a suspect script can really help in debugging.
Be aware some distributions have shell startup scripts so contorted they end up actually defying the order given in the bash(1) manual page. Anytime you find a default user ~/.profile
checking for $BASH or $BASH_VERSION, you may be in one of these, um..., "interesting" environments, and may have to read through them to figure out where the control flow goes (they should be using a bash-specific ~/.bash_profile
or ~/.bash_login
, which includes the more generic ~/.profile
by reference, thus letting the bash executable do the work instead of having to write $BASH checks in shell code).
~/.bash_profile
(or ~/.bash_login
) can certainly include . ~/.bashrc
, but the environment variables belong in the ~/.bash_profile
(if bash-specific) or the ~/.profile
included from it (if you're using this mechanism and have envvars for everything else in there) as DeWitt says, just remember to put the . ~/.bashrc
AFTER the .bash_profile's . ~/.profile
and other environment variables, so that both login and all other invocations of the ~/.bashrc
can rely on the envvars already being set. An Example ~/.bash_profile
:
# .bash_profile
[ -r ~/.profile ] && . ~/.profile # envvars
[ -r ~/.bashrc ] && . ~/.bashrc # functions, per-tty settings, etc.
#---eof
The [ -r ... ] && ...
works in any Bourne shell descendent and doesn't cause errors/aborts if the .profile is missing (I personally have a ~/.profile.d/*.sh
setup as well, but this is left as an entirely optional exercise).
Note that bash only reads the first file of these three which it finds:
~/.bash_profile
~/.bash_login
~/.profile
...so once you have that one, the use of the other two is entirely under control of the user, from bash's perspective.