linux: how to permanently and globally change environment variables
I want to do the following for every user that logs into my linux box:
export PATH=$PATH:~/.path
And I don't know how to do that besides manually adding that line in every ~/.bashrc file.
Also, a cron job of mine runs a program and I want (for that cronjob) the PYTHONPATH set to something specific. will any .bashrc files affect the cronjob's environment? How do I change a cronjob's environment.
Also, I am now curious as to how to change what the PATH variable is on startup. Other programs seem to do this when they're installed, so how would I go about doing this?
Solution 1:
Environments for shells
Essentially, anything that runs processes will tend to read a configuration file on starting up, and to affect the environment of that, you need to hit its configuration file.
For user shells, "obvious" places are .profile
, .bashrc
, .bash_profile
(I think) and maybe a couple of others I don't remember. Obviously, more and others if you use zsh
, csh
, tcsh
or whatever as a shell.
There are initialization files read by your windowing environment, which may be either KDE or Gnome. The particular window manager you run underneath that may also read a config file. I admit I don't know the names of those files even for my own installation.
Finally, there are usually "master" configuration files for all those environments somewhere in /etc
. They provide defaults for stuff the users don't.
I think that programs that install themselves conscientiously check the various possibilities. Various Linux distributions may offer some helper scripts for this.
cron
This one is a lot easier. For security reasons, cron
only passes a couple of environment variables to subprocesses, ALWAYS. I think USER
is one of those, and MAILTO
another. As far as I know, there's no PATH
set - this often annoys newbies. The environment of a cron job is completely different from your shell environment! Anything you want in the environment, you either pass in on the command line in crontab
, or you start up a script and let that set up whatever environment it needs.
Solution 2:
To apply a bashrc change to all users, you can modify /etc/bash.bashrc
(This is for Ubuntu).
Also, as indicated in the answer above, make sure that this file is sourced by /etc/profile
.