Setting environment variables in Linux for all processes

Processes (including shells) only inherit variables from their parents. There is no way to change them from the outside thereafter.

Set "global variables" as early in the boot (for system processes) or shell invocation (for user processes) as possible, or resign yourself to setting them in multiple places.

This may seem like a pain, but the ability to change another processes environment would be a bug, and would introduce all kinds of nasty race conditions.


What are you trying to do that you want "global" environment variables? There may be way to work around your problem.

Workaround:: Write a minimal script in your favorite shell format that only sets the variable you want and put it in a globally accessible place:

/etc/loglocaltion.sh:

export MY_LOG_DIR=/opt/share/mylog
export MY_DEFAULT_LOG_LEVEL=URGENT

and for everything that you want to use that configuration do one of

  1. launch it from shell which has sourceed loglocation.sh in it's non-interactive login file (the one that gets read for all instances of the shell, i.e. .bash_profile).
  2. write a minimal wrapper script which sources loglocation.sh before launching the real program. launchcorelogger.sh:

 

 CORELOGGER=/opt/sbin/mycorelogger
 source /etc/loglocation.sh
 exec $CORELOGGER

and have init run the script.

Now edits to the loglocation script will effect all associated processing if your run them from a fresh shell in the first case or restart them using you systems dameon restarted (/etc/init.d/mycorelogger restart or whatever).


I propose putting your env vars in /etc/environment. Mine currently contains

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
NUKE_PATH=/spr/studio/codebase/nuke

and NUKE_PATH is set for cron processes and Bash shells.

(Sadly, not for processes started by Deadline, a process queuing app, but that's my problem not yours :-)