LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Setting global system variables (https://www.linuxquestions.org/questions/linux-newbie-8/setting-global-system-variables-299615/)

AzidStar 03-09-2005 10:03 AM

Setting global system variables
 
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

Mara 03-09-2005 11:35 AM

Variables are set when user logs in (or runs a terminal emulator etc). There's no way provided to change variables in all sessionsa fter they're run. What you can think of is editing files in /proc/ (process files), but it may not work and cause problems.

AzidStar 03-10-2005 01:26 AM

Hmm .. I was thinking of using proc earlier.. can I just create new files there (read: in the RAM) or do I have to use the existing file structure?
Sorry if this seems a bit neewbish but we alll have to learn the hard way.

Sincerly Kristoffer Nordström

Mara 03-10-2005 02:04 PM

You need to use the existing files you want to change something - proc is an interface between the kernel and user space and it requires proper formats.

AzidStar 03-11-2005 07:12 AM

Thanks for the reply. Well its back to the drawing board then.
For now I have written a library that "sets" the "variable"-information in /tmp/THISVARIABLE to have a common point for all shells and processes to store and retrieve.
However I will look further into the possibility to use some form of shared memory where variables can be set.
HopeI dont have to write the damn thing myself.

Anyway thanks for the help.

Sincerly Kristoffer Nordström

wpn146 03-11-2005 09:03 AM

Re: Setting global system variables
 
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:
Code:

source filename.sh
-- instead of --
Code:

./filename.sh
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.)
Code:

`./setup_env.sh`
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.


All times are GMT -5. The time now is 12:53 AM.