Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set environment variables in C

Is there a way to set environment variables in Linux using C?

I tried setenv() and putenv(), but they don't seem to be working for me.

like image 707
iman453 Avatar asked Aug 05 '10 15:08

iman453


People also ask

What is environmental variables in C?

Environment variable is a global variable that can affect the way the running process will behave on the system.

How do I set an environment variable?

On the Windows taskbar, right-click the Windows icon and select System. In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables. Click New to create a new environment variable.

How can you set environment variables from the command line?

To set an environment variable, use the command " export varname=value ", which sets the variable and exports it to the global environment (available to other processes). Enclosed the value with double quotes if it contains spaces. To set a local variable, use the command " varname =value " (or " set varname =value ").


2 Answers

I'm going to make a wild guess here, but the normal reason that these functions appear to not work is not because they don't work, but because the user doesn't really understand how environment variables work. For example, if I have this program:

int main(int argc, char **argv) {   putenv("SomeVariable=SomeValue");   return 0; } 

And then I run it from the shell, it won't modify the shell's environment - there's no way for a child process to do that. That's why the shell commands that modify the environment are builtins, and why you need to source a script that contains variable settings you want to add to your shell, rather than simply running it.

like image 116
Carl Norum Avatar answered Oct 09 '22 02:10

Carl Norum


Any unix program runs in a separate process from the process which starts it; this is a 'child' process.

When a program is started up -- be that at the command line or any other way -- the system creates a new process which is (more-or-less) a copy of the parent process. That copy includes the environment variables in the parent process, and this is the mechanism by which the child process 'inherits' the environment variables of its parent. (this is all largely what other answers here have said)

That is, a process only ever sets its own environment variables.

Others have mentioned sourcing a shell script, as a way of setting environment variables in the current process, but if you need to set variables in the current (shell) process programmatically, then there is a slightly indirect way that it's possible.

Consider this:

% cat envs.c #include <stdio.h> int main(int argc, char**argv) {     int i;     for (i=1; i<argc; i++) {         printf("ENV%d=%s\n", i, argv[i]);     } } % echo $ENV1  % ./envs one two ENV1=one ENV2=two % eval `./envs one two` % echo $ENV1 one %  

The built-in eval evaluates its argument as if that argument were typed at the shell prompt. This is a sh-style example; the csh-style variant is left as an exercise!

like image 45
Norman Gray Avatar answered Oct 09 '22 03:10

Norman Gray