Quote:
Originally posted by AzidStar
Hello everyone.
I was wondering how to set environmet variables that are globally accessible for all processes/users that are in the system.
I saw that you can do this by staticly adding line to /etc/profile, but I want to be able to do this "on the fly" and preferably from a c-program or secondly from a shell script.
The problem right now is that when I create a variable in one shell (read one memory space) it is only visible to that shell.
How are the global variables set so that everyone can access them?
I hope I am making some sence to you out there.
Sincerly Kristoffer Nordstr�m
|
If you use:
-- instead of --
then its environment variables will be set in your current environment.
For more complex situations where you need to execute a new process to create environment variables, then they will not exist after the process terminates. One trick to get around this is to have the process write its output to stdout, then "backtick" or "$(...)" the execution of the script into your current shell.
A simple example -- assume script is named "setup_env.sh" (this is just an example, for something this simple you would just "source" a script.)
Code:
#!/bin/bash
/bin/echo "export aaa=aaa"
/bin/echo "export bbb=bbb"
/bin/echo "export ccc=ccc"
Then execute it with: (note: there are "backticks" around the command. They are not particles of dust on your screen.)
Result:
Code:
bash-2.05b$ echo "$aaa $bbb $ccc"
aaa bbb ccc
This execute line could also be placed into /etc/profile for global usage. Instead of a simple shell script, this could be quite a complex perl script or compiled C program if you wish. Also handy is that during debug, you can execute it without the backticks (or $(...)) and it will just print out what it would do if executed in backticks.