Solved

How do I set a Linux environment variable from within C/C++

Posted on 2004-08-07
6
13,419 Views
Last Modified: 2008-01-09
Hi,

I wish to set a Linux environment variable from within GNU C/C++.

I've tried "system("var=hello"); but this doesnt work - the variable seems to disappear when I check it in a shell script.
0
Comment
Question by:Risky101
6 Comments
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
man setenv

       #include <stdlib.h>

       int setenv(const char *name, const char *value, int overwrite);
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
the variable seems to disappear when I check it in a shell script
you cannot set an variable from within your program which is still alive after this program exits
The only way to do this is to exit your program with any of the exec*() function, calling another program or shell
0
 
LVL 8

Expert Comment

by:akshayxx
Comment Utility
i would agree with ahoffmann,  env variables can be passed on to children processes, but not escalated up to parents., hence in the C program, whatever u set , will die once the program dies.. unless u exec some shell/program from there in.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:Risky101
Comment Utility
the program 'read' (built into Bash) manages to set the environment variable properly, ie:

read yesno
if [ "$yesno" = "y" ];then
  # do something
fi

So far, I've tried:
system("yesno=\"n\");
setenv()
system ("export etc etc");
system ("declare etc etc");

Without exception, the variables disappear when the program exits. Is there any way, any at all, to set an environment variable, just like read does?

0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
>  the program 'read' (built into Bash) manages to set the environment variable properly, ie:
NO.

> Without exception, the variables disappear when the program exits.
yes, as already said.
0
 
LVL 20

Accepted Solution

by:
jmcg earned 500 total points
Comment Utility
Environment variables are process-based. A process can only change environment variables for itself and, by inheritance, for its children.

Any invocation of 'system' creates at least one additional process. Any changes that process makes to its environment are lost when it exits and most certainly cannot effect the environment variables of the parent.

A subprocess can produce, as an output, an instruction for changing the parent's environment variable. And as you've experienced with the 'read' built-in, the shell can change its own environment variables and pass them along to its children.

So, the usual way to have a program return a value into a shell environment variable looks something like this:

result=`external-command`
export result

You can also have the program write its new settings for environment variables into a file

external-command >new-env

where the new-env file ends up looking like:

env1=value1
export env1
env2=value2
export env2

and the shell can then use the "." command to read in the new-env file to make the settings effective

. new-env

======

None of this is particular helpful if the only thing you have control over is a C/C++ program. The shell that calls your program has to cooperate if its environment variables are to be changed.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now